120 likes | 231 Views
LIS651 lecture 8 functions. Thomas Krichel 2010-10-18. functions. The PHP function reference is available on its web site http://php.net/quickref.php. It shows the impressive array of functions within PHP.
E N D
LIS651 lecture 8functions Thomas Krichel 2010-10-18
functions • The PHP function reference is available on its web site http://php.net/quickref.php. It shows the impressive array of functions within PHP. • But one of the strengths of PHP is that you can create your own functions as you please. • If you recreate one of the built-in functions, your own function will have no effect.
simplest function function beer_print () { print "beer\n"; } beer_print() ; // prints: “beer” and newline The set of arguments is empty.
A more practical example • Stephanie Rubino was an English teacher and objects to sentences like You have ordered 1 bottles of Grosswald Pils. • Let us define a function rubino_print(). It will take three arguments • a number to check for plural or singular • a word for the singular • a word for the plural
a function and its arguments • declare the arguments to the function in parenthesis function rubino_print ($number, $singular,$plural) { if($number == 1) { print "one $singular"; } else { print "$number $plural"; } } rubino_print(3,'woman','women'); // prints: “3 women”
default arguments • Sometimes you want to allow a function to be called without giving all its arguments. You can do this by declaring a default value, using an equal sign in the function list function thomas_need($thing='beer') { print "Thomas needs $thing.\n"; } thomas_need(); // prints: “Thomas needs beer.” thomas_need('vodka'); // prints: “Thomas needs vodka”.
rubino_print using common plurals function rubino_print ($num, $sing,$plur=1) { if($num == 1) { print "one $sing"; } elseif($plur ==1) { print "$num $sing"."s"; } else { print "$num $plur"; } } rubino_print(6,'beer'); // prints: “6 beers”
return value • Up until now we have just looked at the side effect of a function. • "return" is a special command that returns a value. • It takes the return value as a parameter return $result; • When return is used, the function is left. Example function good_beer () { return 'Festbock'; } $beer=good_beer(); print $beer ; // prints: “Festbock”.
rubino_print with return function rubino_print ($number, $singular,$plural) { if($number == 1) { return "one $singular"; } return "$number $plural"; } $order=rubino_print(2,"beer","beers"); print "you ordered $order\n"; // prints: you ordered 2 beers.
visibility of variables • Variables used inside a function are not visible from the outside. Example $beer="Karlsberg"; function yankeefy ($name='Sam Adams') { $beer=$name; } yankeefy(); print $beer; // prints: Karlsberg • The variable inside the function is something different than the variables outside.
accessing global variables. • There are two ways to change a global variable, i.e. one that is defined in the main script. • One is just to call it as $GLOBALS['name'] wherename is the name of the global variable. function yankeefy ($name="Sam Adams"){ $GLOBALS['beer']=$name; } • The other is to change it outside a function definition. • Example in brewer_quiz.php
http://openlib.org/home/krichel Thank you for your attention! Please switch off machines b4 leaving!