Date post: | 15-Apr-2017 |
Category: |
Technology |
Upload: | ahmed-swilam |
View: | 495 times |
Download: | 0 times |
PHP Functions
Outline• Functions• Function declaration• Function Arguments• Variable number of arguments• Returning Values• Variable Scope• Static variables• Recursion• Tricks• Useful Built-in Functions• Assignment
Functions• Is a sub program.• A block of code that is defined under a certain name
( function name ).• Can take arguments.• Can return a value.• Function name has the same naming rules as variables
and constants.• Function names are not case sensitive.
Function declaration
function doSomething($arg_1, $arg_2, /* ..., */ $arg_n){ echo “something.\n"; return $val;}
Name Arguments
Body
Return
Function Arguments• Passing by value (default – except for objects) :
function multiply($x, $y){ echo $x * $y;}
Function Arguments• Passing by reference :
$x = 10;$y = 5;$result = 0;multiply( $x, $y , $result );echo $result; // 50
function multiply($x, $y, &$result){ $result = $x * $y;}
Function Arguments• Default argument values:
function doIt( $name = “PHP” ){ echo “Hello ” . $name;}Can be called :
doIt(); // Hello PHP doIt( “Guys” ); // Hello Guys
• It is preferable that the arguments with default values should be the last arguments passed to the function.
Function Arguments• PHP emits a warning if the number of the arguments
passed is not the same the number of arguments in the function declaration.
• PHP does not throw any warning in case the number is bigger than the arguments in the function declaration.
function doIt( $name ){ echo “Hello ” . $name;}doIt(); // a warning is emitteddoIt( “Elephant”, “Cow” ); // this is OK
Demo
Variable number of arguments• PHP allows you to create a function with unlimited
number of arguments.function displayAll( ){ $numargs = func_num_args();
$arg_list = func_get_args();for ($i = 0; $i < $numargs; $i++) {
echo $arg_list[$i] . “ ”;}
}displayAll( “This” , “is”, “a”, “good”, “thing.” );
Returning Values• Functions may return values :
function doIt( $name ){ return “Hello ” . $name;}
echo doIt(“PHP”); // Hello PHP
Variable Scope
$a = 1; /* global scope */ function test(){ echo $a; /* reference to local scope variable */ }
test(); // echoes an empty string
Variable ScopeThe GLOBAL keyword:
$a = 1; /* global scope */ function test(){ global $a; // can also use the $GLOBALS array echo $a; /* reference to local scope variable */ }
test(); // 1
Static variables• Static variables are initialized only one time.
function test(){ static $a = 0; echo $a; $a++;}test(); //0test(); // 1test(); // 2
Recursion• Recursion occurs when something contains, or uses, a
similar version of itself.• In programming, it happens when a function is having
a call to itself.• The factorial problem :• 5! = 5 x 4 x 3 x 2 x 1 = 120
Recursionfunction factorial($number) {
if ($number == 0) return 1; return $number * factorial($number - 1); }
echo factorial(6); // 720
TricksA function inside an IF statement :
if ($condition) { function doSomething() { echo “Will only be defined if the IF condition is true"; } // some code doSomething();
}
TricksA function inside another function:
function doSomething1() {
function doSomething2() { echo "I can only be called from inside doSomething1 \n"; } // some code doSomething2();
}
TricksVariable functions:
function display( $x ) {
echo $x; }
$name = “display”;$name( “PHP” ); //PHP
TricksAnonymous functions:
$greet = function($name){ echo “Hello “ . $name;};
$greet(“PHP”); // Hello PHP
Useful Built-in Functions• boolean function_exists( $name )• Checks whether there is a function with that
$name.• void get_defined_functions()• Gets a list of all names of defined functions in the
script.
Assignment- Write a PHP function that calculates the average of passed parameters (The number of parameters is dynamic).
What's Next?• Arrays.
Questions?