·  PHP – Syntax – Functions

 

- Info:

  - This tutorial shows how to create your own functions in PHP and how to use them.

 

- With No Parameters:

  - This example shows how to create and call function that has no input parameters.

  - Create PHP Console Application Test.php.

 

    Test.php

    <?php

      test();                                #Call function test with no parameters.

        

      function test() {                      #Define function test that has no input parameters.

        print("Hello from test.");

      }

    ?> 

 

- With Parameters – By Value:

  - This example shows how to create and call function whose input parameters are given by value.

  - This means that any changes made to those values within the scope of the function are ignored outside of the function.

  - Create PHP Console Application Test.php.

 

    Test.php

    <?php

      test("John", 10);                      #Call function with input parameters.

        

      function test($name, $id) {

        print("Hello ".$name.". You're id is ".$id.".");

      }

    ?> 

 

- With Parameters – By Reference:

  - This example shows how to create and call function whose input parameters are given by reference.

  - This means that any changes made to those values within the scope of the function are reflected outside of the function.

  - Create PHP Console Application Test.php.

 

    Test.php

    <?php

   

      $name = "John";

      test($name);                     

      echo("Hello ".$name);   //Hello Jill.

        

      function test(&$name) {

        $name = "Jill";

      }

      

    ?> 

 

- With Variable Number of Parameters:

  - This example shows how to create and call function that has variable number of input parameters.

  - Optional variables must have defined default values and must be defined after required variables.

  - Default values must be constant expressions, you cannot assign function calls or variables.

  - Create PHP Console Application Test.php.

 

    Test.php

    <?php

      test("John", "Carpenter");             #Call function with input parameters.

        

      function test($name, $surname, $id=10) {

        print("Hello $name $surname. You're id is ".$id.".");

      }

    ?>  

 

- With Return Value – Single:

  - This example shows how to return value from a function by using return statement.

 

    Test.php

    <?php

      $result = test();                      #Store function return value into scalar $result.

      print($result);

        

      function test() {

        return "Hello from PHP.";            #Return string.

      }

    ?> 

 

- With Return Value – Multiple:

  - This example shows how to return multiple values from a function by returning array and then using list() to load array

    elements into variables.

 

    Test.php

    <?php

   

      list($name, $email, $language) = retrieveUserProfile();

      echo "Name: $name, email: $email, language: $language"

   

      function retrieveUserProfile() {

        $user[] = "Jason";

        $user[] = "jason@example.com";

        $user[] = "English";

        return $user;

      }

        

    ?>