1 / 12

LIS651 lecture 8 functions

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.

avak
Download Presentation

LIS651 lecture 8 functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LIS651 lecture 8functions Thomas Krichel 2010-10-18

  2. 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.

  3. simplest function function beer_print () { print "beer\n"; } beer_print() ; // prints: “beer” and newline The set of arguments is empty.

  4. 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

  5. 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”

  6. 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”.

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

  8. 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”.

  9. 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.

  10. 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.

  11. 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

  12. http://openlib.org/home/krichel Thank you for your attention! Please switch off machines b4 leaving!

More Related