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

09/03/2014 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:

 

 

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:

 

 

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:

 

 

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:

 

 

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:

 

 

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

 

A video tutorial demonstrating functions in PHP:

 

Related Links:

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 *

Scroll Up