PHP Functions (Part 5 of 5) Slide 1
CHAPTER 2:
PHP Functions (5 of 5)
Topics covered:-
Functions
Optional parameters and default values
Global variables
Superglobal arrays
Static variables
Working with reference
AMIT 2043 Web Systems and Technologies
PHP Functions (Part 5 of 5) Slide 2
Introduction to Functions
• Also called subroutine or methods.
• It contains a block of code that perform a specific task.
• A function can accepts one or more arguments. These
arguments can be of value type or reference type.
• A function can optionally return values.
• Can you elaborate why functions are so important and useful
in any programming languages?
• Note: Since you have learnt C programming language to
declare, define and call function, the concept is also the same
in PHP.
AMIT 2043 Web Systems and Technologies
PHP Functions (Part 5 of 5) Slide 3
Functions
• Function declaration and definition.
• Syntax:
functionName (arg1, arg2, arg3, …){
//optional return statement
//if the function does return a value
return
}
• Function call that returns a value.
• Syntax:
$valueReturned = functionName(arg1, arg2, arg3, …);
functionName(arg1, arg2, arg3, …);
AMIT 2043 Web Systems and Technologies
PHP Functions (Part 5 of 5) Slide 4
Optional Parameters and Default Values
• Example:
echo "Calculate an area of a square<br>";
echo calSquareArea(2,3) . "<br>";
echo calSquareArea(2) . "<br>";
function calSquareArea($width, $height=2){
return $width * $height;
}
AMIT 2043 Web Systems and Technologies
PHP Functions (Part 5 of 5) Slide 5
Working with Global Variables
• A global variable is a variable that you declared which can be
accessible anywhere in your script whether it is inside or
outside the function.
• Global variables available in PHP is somewhat different with
global variables that you learnt in your C programming
language.
• A variable that you define outside a function, is in fact a global
variable.
• However, in order to access global variable from within a
function, the keyword global must be used followed by the
name of that variable.
AMIT 2043 Web Systems and Technologies
PHP Functions (Part 5 of 5) Slide 6
Working with Global Variables
• Example:
$count = 7;
function displayCount(){
global $count;
echo $count;
}
displayCount(); // display 7
• However, if global was not supplied before $count, an error will
be returned as follows:-
AMIT 2043 Web Systems and Technologies
PHP Functions (Part 5 of 5) Slide 7
Working with $GLOBALS[] Array
• Global variables can also be accessed using the $GLOBALS array.
• This is a special type of array called superglobal, meaning you can access it
anywhere using global statement.
• This superglobal variable contains a list of all global variables, with variable
names stored as key and variable values stored as values.
• Example:
$count = 10;
function displayCount(){
echo $GLOBALS["count"];
}
No dollar ($) symbol
displayCount(); //display 10
AMIT 2043 Web Systems and Technologies
PHP Functions (Part 5 of 5) Slide 8
Static Variables
• Variables declared in a function often been wiped off when the function exit.
• Sometimes, it is often useful to retain local variables’ values even after the
function ends.
• In order to preserve local variables’ values, use static keyword.
• Example:
function displayCounter(){
static $count = 0;
return ++$count;
}
echo "I have counted to : " . displayCounter() . "<br>";
echo "I have counted to : " . displayCounter() . "<br>";
echo "I have counted to : " . displayCounter() . "<br>";
echo "I have counted to : " . displayCounter() . "<br>";
echo "I have counted to : " . displayCounter() . "<br>";
AMIT 2043 Web Systems and Technologies
PHP Functions (Part 5 of 5) Slide 9
Working with References
• Whenever a value is passed as an argument into the function, the function will
only gets a copy of that value.
• In case, you would want to change the original value, it is advisable to pass this
value into the function argument by reference. Use the ampersand (&) symbol.
• Example:
function resetCounter($c){
$c = 0;
}
$counter = 0;
++$counter;
++$counter;
++$counter;
++$counter;
echo $counter; //display 4
resetCounter($counter); //should display 0, but it display 4 again
echo $counter;
AMIT 2043 Web Systems and Technologies
PHP Functions (Part 5 of 5) Slide 10
Working with References
• Whenever a value is passed as an argument into the function, the function will
only gets a copy of that value.
• In case, you would want to change the original value, it is advisable to pass this
value into the function argument by reference. Use the ampersand (&) symbol.
• Example:
function resetCounter(&$c){
$c = 0;
}
$counter = 0;
++$counter;
++$counter;
++$counter;
++$counter;
echo $counter; //display 4
resetCounter($counter); //display 0
echo $counter;
AMIT 2043 Web Systems and Technologies
PHP Functions (Part 5 of 5) Slide 11
Returning References From Function
• Simply just add an ampersand symbol (&) in front of the function name.
• Example:
$count = 0;
function &ref(){
global $count;
return $count;
}
$countNext = &ref();
$countNext++;
echo $count;
echo $countNext;
• Guess the answer returned by the two echo statement.
AMIT 2043 Web Systems and Technologies