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 emitted doIt( “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 Scope The 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(); //0 test(); // 1 test(); // 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
Recursion function factorial($number) { if ($number == 0) return 1; return $number * factorial($number - 1); } echo factorial(6); // 720
Tricks A function inside an IF statement : if ($condition) { function doSomething() { echo “Will only be defined if the IF condition is true"; } // some code doSomething(); }
Tricks A function inside another function: function doSomething1() { function doSomething2() { echo "I can only be called from inside doSomething1 n"; } // some code doSomething2(); }
Tricks Variable functions: function display( $x ) { echo $x; } $name = “display”; $name( “PHP” ); //PHP
Tricks Anonymous 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?

Class 3 - PHP Functions

  • 1.
  • 2.
    Outline • Functions • Functiondeclaration • Function Arguments • Variable number of arguments • Returning Values • Variable Scope • Static variables • Recursion • Tricks • Useful Built-in Functions • Assignment
  • 3.
    Functions • Is asub 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.
  • 4.
    Function declaration function doSomething($arg_1,$arg_2, /* ..., */ $arg_n) { echo “something.n"; return $val; } Name Arguments Body Return
  • 5.
    Function Arguments • Passingby value (default – except for objects) : function multiply($x, $y) { echo $x * $y; }
  • 6.
    Function Arguments • Passingby reference : $x = 10; $y = 5; $result = 0; multiply( $x, $y , $result ); echo $result; // 50 function multiply($x, $y, &$result) { $result = $x * $y; }
  • 7.
    Function Arguments • Defaultargument 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.
  • 8.
    Function Arguments • PHPemits 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 emitted doIt( “Elephant”, “Cow” ); // this is OK
  • 9.
  • 10.
    Variable number ofarguments • 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.” );
  • 11.
    Returning Values • Functionsmay return values : function doIt( $name ) { return “Hello ” . $name; } echo doIt(“PHP”); // Hello PHP
  • 12.
    Variable Scope $a =1; /* global scope */ function test() { echo $a; /* reference to local scope variable */ } test(); // echoes an empty string
  • 13.
    Variable Scope The GLOBALkeyword: $a = 1; /* global scope */ function test() { global $a; // can also use the $GLOBALS array echo $a; /* reference to local scope variable */ } test(); // 1
  • 14.
    Static variables • Staticvariables are initialized only one time. function test() { static $a = 0; echo $a; $a++; } test(); //0 test(); // 1 test(); // 2
  • 15.
    Recursion • Recursion occurswhen 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
  • 16.
    Recursion function factorial($number) { if($number == 0) return 1; return $number * factorial($number - 1); } echo factorial(6); // 720
  • 17.
    Tricks A function insidean IF statement : if ($condition) { function doSomething() { echo “Will only be defined if the IF condition is true"; } // some code doSomething(); }
  • 18.
    Tricks A function insideanother function: function doSomething1() { function doSomething2() { echo "I can only be called from inside doSomething1 n"; } // some code doSomething2(); }
  • 19.
    Tricks Variable functions: function display($x ) { echo $x; } $name = “display”; $name( “PHP” ); //PHP
  • 20.
    Tricks Anonymous functions: $greet =function($name) { echo “Hello “ . $name; }; $greet(“PHP”); // Hello PHP
  • 21.
    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.
  • 22.
    Assignment - Write aPHP function that calculates the average of passed parameters (The number of parameters is dynamic).
  • 23.
  • 24.