Advance PHP Function

More than 1000 built-in functions prove the power of PHP. Besides built-in function PHP allows to create user defined function that was explained in PHP function basics. So, this article is designed to explain some advance topics on PHP function.

PHP function Advance Topics

PHP function Advance Topics

Calling function dynamically

A function can be called dynamically keeping function name into a variable. An example of calling a PHP function dynamically is given below…


<?php

function show(){

echo("Example to call a function dynamically");

}

$show_result="show";

$show_result();

?>

Output: Example to call a function dynamically.

Here, the name of the show() function is being stored into $show_result variable and then the function is being called dynamically using the variable.

Variable Area in PHP function

There are two kinds of variable in PHP. These are..

  1. Global variable: These variables are accessed from anywhere in a page.
  2. Local variable: These variables are accessed only in a block of statement.

If you declare a variable at starting area of a page, the variable will be global variable and this variable can be accessed from anywhere in that page. On the other hand, if you declare a variable within a function, the variable will be local variable. After executing the function, the variable will be erased from the memory and you cannot reuse that variable anywhere in the page. If you use any local variable outside the function, PHP compiler will show an error message. See below example carefully…


<?php

$a=5;

$b=10;

function show(){

$a=15;

$b=20;

echo "Local variable a = ".$a." and b = ".$b;

}

show();

echo "&amp;lt;br&amp;gt;Global variable a= ".$a." and b = ".$b;

?>

Output

Local variable a=15 and b=20

Global variable a=5 and b=10

Here, two variables with same name are being declared but one is globally and another is locally. The show() function is showing the local variable value but outside the function the global variable’s value is being used. The local variable does not affect the global variable.

Using Global Variable in Function

Normally PHP function uses local variable. If a global variable is accessed within a function, PHP compiler will show a notice like undefined variable: calling_variable. So, to use a global variable in a function, the global variable must declare within the function with global keyword. See below example…


<?php

$a=5;

$b=10;

function show(){

global $a;

$a=15;

$b=20;

echo "Local variable a = ".$a." and b = ".$b;

}

show();

echo "<br>Global variable a is now ".$a." and b = ".$b;

?>

Output

Local variable a=15 and b=20

Global variable a is now 15 and b=10

The global variable $a is declared within function with the global keyword. So, the global variable is now being used in the function and the value is being changed. Again, the variable $b is also declared in the function but without global keyword. So, the value of global variable $b is not being changed because the variable $b is using as local variable in the function.

Making Variable Static within Function

Normally local variable will be erased after executing any function. But local variable can be stored using static keyword. The static keyword makes any variable static storing the variable. The value of a static variable is assigned only once in a function. See below example…


<?php

function show(){

static $count=0;

$count++;

print("show called ".$count." times <br>");

}

show();

show();

show();

?&amp;gt;

Output

show called 1 times

show called 2 times

show called 3 times

Here, the $count variable is a static variable. So, the variable is not being erased calling the function each time. Also the value 0 is assigning only once and each calling the previous value is being stored.

Assigning argument’s default value of a function

If any function takes argument, the argument is assigned at the time of function calling otherwise a warning message is shown. To avoid this, PHP allows to define the default value of an argument. If default value of any argument is assigned, the function can be called without argument. See below example…


<?php

function add($a=5,$b=2){

$c=$a+$b;

return $c;

}

$result1=add();

echo("The result of default value is ".$result1);

$result2=add(10);

echo("<br>The result of one default value and one passed value is ".$result2);

$result3=add(10,5);

echo("<br>The result of two passed values is ".$result3);

?>

Output

The result of default value is 7.
The result of one default value and one passed value is 12.
The result of two passed values is 15.

In this example, The add() function is declared with two arguments. The default value of these arguments are also assigned and the function is calling with three ways…

  1. Function is calling without argument. So, default argument’s value is being used.
  2. Function is calling with one argument. So, one passed value and one default value are being used.
  3. Function is calling with two arguments. So, only passed value is being used.

Function call by value and call by reference

In practical application, a PHP function which has arguments is often called with variables. There are two ways to call a function with variables…

  1. Function call by value
  2. Function call by reference

Function call by value

This is the general form used to call a function in PHP. The function is called with variable argument but after execution the value of that variable remains unchanged. See below example…


<?php

function addTen($num){

$num=$num+10;

}

$oldNum=10;

addTen($oldNum);

print("The variable value is ".$oldNum);

?>

Output

The variable value is 10.

Here, the addTen() function is being called with a variable having value 10. After calling the function, the variable is being printed but remaining unchanged. If the value of this variable need to be changed with calling the function, function call by reference have to be used.

Function call by reference

Calling function by reference, the value of a variable can be changed after execution the function. See below example where function is calling by reference variable…


<?php

function addTen($num){

$num=$num+10;

}

$oldNum=10;

addTen(&$oldNum);

print("The variable value is ".$oldNum);

?>

Output

The variable value is 20.

Here, an ampersand (&) is being used before the variable to call a function by reference. Because of calling the function by reference variable, the value of the variable is being changed.

Again, a function can be declared as call by reference format. By doing this, whenever the function is called with any variable, the value of the variable will be changed. See below example…


<?php

function addTen(&$num){

$num=$num+10;

}

$oldNum=10;

addTen($oldNum);

print("The variable value is ".$oldNum);

?>

Output

The variable value is 20.

Here, the addTen() function is declared as call by reference. As a result, the value of the variable is being changed after calling the function.

Some advance topics on PHP function have been explained in this article. If you face any problem to understand these topics, feel free to contact with me from Contact us page. I’ll try my best to stay with you. PHP array, an important topic in PHP programming, will be discussed in next tutorial. Spend some times to study that article and improve your idea about PHP array data.

One Response
  1. Md Rony

Leave a Comment

Your email address will not be published. Required fields are marked *