Create FREE 'HowTo' Videos with MyGuide

Functions in PHP



Pass Quiz and Get a Badge of Learning



Content "filtered", Please subscribe for FULL access.


Chapter 9 : Functions in PHP



Functions in PHP arrow_upward


  • A function is a block of code which can be called from any point in a script after it has been declared.
  • It is basically a compartmentalized PHP script designed to accomplish a single task.
  • In PHP, there are more than 1000 built-in functions and PHP gives you option to create your own functions as well.

  • Create a PHP Function arrow_upward


  • A function will be executed by a call to the function.
  • Syntax

    
    
    function functionName()
    {
    code to be executed;
    }
    
    
  • The function name can start with a letter or underscore but not with a number.
  • While creating a function its name should start with keyword function and all the PHP code should be put inside {…} braces as shown in the following example:
  • Example

    
    
    <html>
    <head>
    <title>Writing PHP Function</title>
    </head>
    <body>
    <?php
    /* Defining a PHP Function */
     
    function writeMessage()
    {
      echo "It’s Simple<i>N</i>Easy App!";
    }
    /* Calling a PHP Function */
    writeMessage();
    ?>
    </body>
    </html>
    
    

    Output:

  • It’s SimpleN Easy App!

  • Functions with Parameters arrow_upward


  • To add more functionality to a function, we can add parameters.
  • A parameter is just like a variable.
  • PHP gives you option to pass your parameters inside a function.
  • Parameters are specified after the function name, inside the parentheses.
  • Following example takes two integer parameters and adds them together and then prints them.
  • Example

    
    <html>
    <head>
    <title>Writing PHP Function with Parameters</title>
    </head>
    <body>
    <?php
    function addFunction($num1, $num2)
    {
      $sum = $num1 + $num2;
      echo "Sum of the two numbers is : $sum";
    }
    addFunction(10, 10);
    ?>
    </body>
    </html>
    
    

    Output:

    Sum of the two numbers is: 20


    Passing Arguments by Reference arrow_upward


  • It is possible to pass arguments to functions by reference.
  • This means that a reference to the variable is manipulated by the function rather than a copy of the variable's value.
  • Any changes made to an argument in these cases will change the value of the original variable.
  • You can pass an argument by reference by adding an ampersand to the variable name in either the function call or the function definition.
  • Following example depicts both the cases.
  • Example

    
    <html>
    <head>
    <title>Passing Argument by Reference </title>
    </head>
    <body>
    <?php
    function addFive (&$num)
    {
       $num += 5;
    }
    function addSix (&$num)
    {
      $num += 6;
    }
     
    $orignum = 10;
    addFive ($orignum );
    echo "Original Value is $orignum <br/ >";
    addSix ( $orignum );
    echo "Original Value is $orignum <br/ >";
    ?>
    </body>
    </html>
    
    

    Output:

  • Original Value is 15
  • Original Value is 21

  • Function Returning Value arrow_upward


  • To let a function return a value, use the return statement.
  • Return stops the execution of the function and sends the value back to the calling code.
  • A function cannot return multiple values, but similar results can be obtained by returning an array.
  • Following example takes two integer parameters and adds them together and then returns their sum to the calling program.
  • Keyword return is used to return a value from a function.
  • Example

    
    <html>
    <head>
    <title>Writing PHP Function which returns value</title>
    </head>
    <body>
      <?php
    function addFunction ($num1, $num2)
    {
      $sum = $num1 + $num2;
      return $sum;
    }
    $return_value = addFunction (10, 20);
    echo "Returned value from the function :".$return_value;
    ?>
    </body>
    </html>
    
    

    Output:

    Returned value from the function: 30


    Setting Default Values for Function Parameters arrow_upward


  • You can set a parameter to have a default value if the function's caller doesn't pass it.
  • When designing your functions, it is often helpful to be able to assign default values for parameters that aren't passed.
  • Example

    
    <html>
    <head>
    <title>Writing PHP Function which returns value</title>
    </head>
    <body>
     
    <?php
    function printMe ($param = NULL)
    {
       print $param;
    }
    printMe ("This is test");
    printMe ();
    ?>
    </body>
    </html>
    
    

    Output:

  • This is test


  • Thank You from Kimavi arrow_upward


  • Please email us at Admin@Kimavi.com and help us improve this tutorial.


  • Mark as Complete => Receive a Certificate in PHP-Programming


    Kimavi Logo

    Terms and conditions, privacy and cookie policy | Facebook | YouTube | TheCodex.Me | Email Kimavi


    Kimavi - An AI Powered Encyclopedia { Learning is Earning }

    Get Ad Free Encyclopedia with Progress Report, Tutor Help, and Certificate of Learning for only $10 a month



    All videos on this site created using MyGuide.

    Create FREE HowTo videos with MyGuide.