
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Good work for php subject of sem 6
Typology: Cheat Sheet
1 / 32
This page cannot be seen from the preview
Don't miss anything!

























Why Functions?
PHP Documentation - Google
PHP Documentation - Google
Defining Your Own Functions We use the function keyword to define a function, we name the function and take optional argument variables. The body of the function is in a block of code { } function greet() { print "Hello\n"; } greet(); greet(); Hello Hello
Choosing Function Names
Arguments Functions can choose to accept optional arguments. Within the function definition the variable names are effectively “aliases” to the values passed in when the function is called. function howdy($lang) { if ( $lang == 'es' ) return "Hola"; if ( $lang == 'fr' ) return "Bonjour"; return "Hello"; } print howdy('es'). " Glenn\n"; print howdy('fr'). " Sally\n"; Hola Glenn Bonjour Sally
Optional Arguments Arguments can have defaults, and so can be omitted. function howdy($lang='es') { if ( $lang == 'es' ) return "Hola"; if ( $lang == 'fr' ) return "Bonjour"; return "Hello"; } print howdy(). " Glenn\n"; print howdy('fr'). " Sally\n"; Hola Glenn Bonjour Sally
Call By Reference Sometimes we want a function to change one of its arguments, so we indicate that an argument is “by reference” using ( & ). function triple(&$realthing) { $realthing = $realthing * 3; } $val = 10; triple($val); echo "Triple = $val\n"; Triple = 30
http://php.net/manual/en/function.sort.php
Variable Scope
Normal Scope (isolated) function tryzap() { $val = 100; } $val = 10; tryzap(); echo "TryZap = $val\n"; TryZap = 10 http://php.net/manual/en/language.variables.superglobals. Except for $_GET
Global Variables – Use Rarely
Coping with Missing Bits Sometimes, depending on the version or configuration of a particular PHP instance, some functions may be missing. We can check that... if (function_exists("array_combine")){ echo "Function exists"; } else { echo "Function does not exist"; } This allows for evolution.