24
PHP Functions

Class 3 - PHP Functions

Embed Size (px)

Citation preview

Page 1: Class 3 - PHP Functions

PHP Functions

Page 2: Class 3 - 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

Page 3: Class 3 - PHP Functions

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.

Page 4: Class 3 - PHP Functions

Function declaration

function doSomething($arg_1, $arg_2, /* ..., */ $arg_n){ echo “something.\n"; return $val;}

Name Arguments

Body

Return

Page 5: Class 3 - PHP Functions

Function Arguments• Passing by value (default – except for objects) :

function multiply($x, $y){ echo $x * $y;}

Page 6: Class 3 - PHP Functions

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;}

Page 7: Class 3 - PHP Functions

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.

Page 8: Class 3 - PHP Functions

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

Page 9: Class 3 - PHP Functions

Demo

Page 10: Class 3 - PHP Functions

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.” );

Page 11: Class 3 - PHP Functions

Returning Values• Functions may return values :

function doIt( $name ){ return “Hello ” . $name;}

echo doIt(“PHP”); // Hello PHP

Page 12: Class 3 - PHP Functions

Variable Scope

$a = 1; /* global scope */ function test(){ echo $a; /* reference to local scope variable */ }

test(); // echoes an empty string

Page 13: Class 3 - PHP Functions

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

Page 14: Class 3 - PHP Functions

Static variables• Static variables are initialized only one time.

function test(){ static $a = 0; echo $a; $a++;}test(); //0test(); // 1test(); // 2

Page 15: Class 3 - PHP Functions

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

Page 16: Class 3 - PHP Functions

Recursionfunction factorial($number) {

if ($number == 0) return 1; return $number * factorial($number - 1); }

echo factorial(6); // 720

Page 17: Class 3 - PHP Functions

TricksA function inside an IF statement :

if ($condition) { function doSomething() { echo “Will only be defined if the IF condition is true"; } // some code doSomething();

}

Page 18: Class 3 - PHP Functions

TricksA function inside another function:

function doSomething1() {

function doSomething2() { echo "I can only be called from inside doSomething1 \n"; } // some code doSomething2();

}

Page 19: Class 3 - PHP Functions

TricksVariable functions:

function display( $x ) {

echo $x; }

$name = “display”;$name( “PHP” ); //PHP

Page 20: Class 3 - PHP Functions

TricksAnonymous functions:

$greet = function($name){ echo “Hello “ . $name;};

$greet(“PHP”); // Hello PHP

Page 21: Class 3 - PHP Functions

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.

Page 22: Class 3 - PHP Functions

Assignment- Write a PHP function that calculates the average of passed parameters (The number of parameters is dynamic).

Page 23: Class 3 - PHP Functions

What's Next?• Arrays.

Page 24: Class 3 - PHP Functions

Questions?