Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: PHP. No Comments on Functions in PHP

Introduction

 

A function is a part of program which contains set of statements that can perform a desired task. A function can be defined with the function keyword followed by the function name and an optional set of parameters enclosed in parentheses.

 

A function definition may not occur before a function call. It can be anywhere in the script. Although function definitions can be nested, it is not encouraged as they can make the script complex. Syntax for defining a function is given below:

function  function_name([param1, param2, ....])
{
    //Body of the function
    [return expression;]
}

 

The execution of the function terminates whenever a return statement is encountered or whenever the control reaches the end of the function’s body.

 

Parameters

 

As in any typical programming language, parameters in a function definition are known as formal parameters and the parameters in a function call are known as actual parameters. The number of formal parameters and normal parameters may not be equal.

 

The default parameter passing mechanism is pass-by-value where a copy of the actual parameters are passed to actual parameters. Below example demonstrates pass-by-value:

function swap($x, $y)
{
   $temp = $x;
   $x = $y;
   $y = $temp;
}
$a = 10;
$b = 20;
sum($a, $b);

 

After the above code is executed the values of a and b remains 10 and 20 even after the function call.

 

Another way of passing parameters is pass-by-reference where the address of the variable is passed rather than a copy. In this mechanism changes on formal parameters are reflected on actual parameters. The address can be passed using & symbol before the variable as shown below:

function swap(&$x, &$y)
{
   $temp = $x;
   $x = $y;
   $y = $temp;
}
$a = 10;
$b = 20;
sum($a, $b);

 

Pass-by-reference can be used to return multiple values from a function.

 

Scope of Variables

 

Scope refers to the visibility of a variable in the PHP script. A variable defined inside a function will have local scope i.e., within the function. A variable outside the function can have the same name as a local variable in a function, where the local variable has higher precedence over the outer variable.

 

In some cases, a function might need access to a variable declared outside the function definition. In such cases, global declaration can be used before the variable name which allows the accessibility of the variable that is declared outside the function definition. Below example demonstrates local and global variables:

function sum_array($list)
{
    global $allsum; //Global variable
    $sum = 0; //Local variable
    foreach($list as $x)
       $sum += $x;
    $allsum += $sum;
    return $sum;
}
$allsum = 0;
$array1 = new array(10, 20, 30, 40);
$array2 = new array(1, 2, 3, 4);
$ans1 = sum_array($array1);
$ans2 = sum_array($array2);
print("Sum of array1 is: $ans1<br />");
print("Sum of all arrays is: $allsum");

 

In the above example, first print statement gives 100 and the second print statement gives 110.

 

Lifetime of Variables

 

The lifetime of a normal variable in a function is until the function execution completes. Sometimes, there might be a need to retain the value of a variable between function calls. In such cases, the variable must be declared with static. The lifetime of static variable is until the execution of script completes. Below example demonstrates the use of static variables:

function func1( )
{
   static $count = 0;
   $count++;
}
for($i = 1; $i <= 10; $i++)
   func1( );
print("func1 is called $count times.");

 

In the above example, the print statement prints 10 instead of 0.

 

A video tutorial demonstrating functions in PHP:

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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