Tutorial by Examples

A basic function is defined and executed like this: function hello($name) { print "Hello $name"; } hello("Alice");
Functions can have optional parameters, for example: function hello($name, $style = 'Formal') { switch ($style) { case 'Formal': print "Good Day $name"; break; case 'Informal': print "Hi $name"; break; ...
Function arguments can be passed "By Reference", allowing the function to modify the variable used outside the function: function pluralize(&$word) { if (substr($word, -1) == 'y') { $word = substr($word, 0, -1) . 'ies'; } else { $word .= 's'; } } $w...
5.6 PHP 5.6 introduced variable-length argument lists (a.k.a. varargs, variadic arguments), using the ... token before the argument name to indicate that the parameter is variadic, i.e. it is an array including all supplied parameters from that one onward. function variadic_func($nonVariadic, ...$...
Variables inside functions is inside a local scope like this $number = 5 function foo(){ $number = 10 return $number } foo(); //Will print 10 because text defined inside function is a local variable

Page 1 of 1