Function in PHP

Admin   PHP   428  2020-11-06 22:07:41

Function in PHP are similar to other programming languages. A function is a piece of code that takes one or more input in a sample of parameters, and performs some processing and returns a value.

In the previous chapter we were familiar with functions like fopen() and fread() ... They are built-in functions, but PHP also gives you the option to create your own function.

This chapter will help you understand the following two parts:

  • Create a function in PHP
  • Call a function in PHP

You hardly ever need to create your own PHP functions, because there are already more than 1000 functions built into the library to handle various jobs and you just need to call them according to your needs.

Refer to Function Summary in PHP for a full range of functions that are useful.

Create functions in PHP

In PHP, it's very simple to create your own function. Let's say you want to create a function, which will simply write a message in the browser when you call it. Following example will create a function writeMessage() and call it after creation.

Note that while creating a function, the function name should start with the function keyword and all PHP code should be enclosed in {and} as follows:

Save the above program in a file called test.php in htdocs, then open a browser and type http://localhost:8080/test.php will produce the following result:

<html>

   <head>

      <title> Functions in PHP </title>

   </head>

   

   <body>
    
      <?php

         / * Function Definition * /

         function blogdevMessage()

         {

            echo "wish you all good study!";

         }


         / * Call the function * /

         blogdevMessage();

      ?>

      

   </body>

</html>

Functions with Parameters in PHP

PHP gives you the option to pass parameters into a function. You can use as many parameters as you like. These parameters work as variables within the function. The following example uses 2 integer parameters and calculates them and prints them.

<html>
   <head>
      <title> PHP Function Example </title>
   </head>
   

   <body>
      <?php

         function sumAll($ num1, $ num2)
         {

            $sum = $num1 + $num2;

            echo "The sum of two numbers is: $sum";

        }
         
        sumAll(11, 22);

      ?>

   </body>

</html>

Save the above program in a file called test.php in htdocs, then open a browser and type http://localhost: 8080/test.php will produce the following result:

Passing parameters by reference in PHP

It is possible to pass parameters to the function by reference. That is, a reference to the variable is manipulated by the function instead of copying a variable's value.

Any changes made to a parameter in these cases will change the value of the original variable. You can pass a parameter by reference by adding a notation & for the variable name: function call or function definitions.

The following example describes these two scenarios:

<html>

<head>

      <title> Passing parameters by reference in PHP </title>

   </head>

   <body>

      <?php

         function addFive($num)

         {

            $num += 5;

         }

         

         function addSix(&$num)

         {

            $num += 6;

         }

         $orignum = 10;

         addFive($orignum);

         echo "The value of the variable orignum is: $orignum <br />";

         addSix($orignum);

         echo "The value of the variable orignum is: $orignum <br />";

      ?>
   </body>

</html>

Save the above program in a file called test.php in htdocs, then open a browser and type http://localhost: 8080/test.php will produce the following result:

Function return value in PHP

A function can return a value using the return statement in conjunction with a value or an object. The return function in PHP stops the execution of the function and sends the value back to the calling code.

You can return more than one value from a function using return array (1,2,3,4).

The following example takes two integers and sums their values, and then returns the result to the calling program. Note that the return keyword is used to return a value from a function.

<html>

   <head>

      <title> The function returns a value in PHP </title>

   </head>

   
   <body>
   

      <?php

         function sumAll($num1, $num2)

         {

            $sum = $num1 + $num2;

            return $sum;

         }

         $result = sumAll(10, 20);

         

         echo "The return value from the function is: $result ";

      ?>

      

   </body>

</html>

Save the above program in a file called test.php in htdocs, then open a browser and type http://localhost:8080/test.php will produce the following result:

 

Set default value for function parameters in PHP

You can set a parameter to a default value if the function caller does not pass it.

The following function prints the default Value string in case it doesn't pass any value to this function.

<html>
   <head>

      <title> Sets default values ​​for function parameters in PHP </title>

   </head>
   <body>

      

      <?php

         function printMessage($param = "Default Value")

         {

            print $param;

         }

         printMessage("Give the function any parameter !!! <br />");

         printMessage();

      ?>

      

   </body>

</html>

Save the above program in a file called test.php in htdocs, then open a browser and type http://localhost:8080/test.php will produce the following result:

Call dynamic functions in PHP

In PHP, you can assign function names as strings to variables and then treat these variables as if you had the function name itself. The following example describes this behavior:

<html>
   <head>

      <title> Call the function dynamically in PHP </title>

   </head>

  
   <body>

      

      <?php

         function sayHello()

         {

            echo "Hello PHP <br />";

         }

         $goiHamDong = "sayHello";

         $goiHamDong();

      ?>

   </body>

</html>

Save the above program in a file called test.php in htdocs, then open a browser and type http://localhost:8080/test.php will produce the following result