function function_name([$arg1 [, $arg2, ..., [$argN]]]) { [statement] [return $return_value;] } Functions are groups of statements that act together; you can call functions and pass data to them, and they can return results to you. Any valid PHP code can be placed inside a function, including other functions and PHP class definitions. Here's an example: function echoer() { echo "No worries."; } When called, this function echoes "No worries.". Here's how you call it from other code: echoer(); You can also pass data to functions as arguments by enclosing them inside parentheses. Here's an example that passes an array to a function, which just echoes the contents of the array: <?php $vegetables[0] = "corn"; $vegetables[1] = "squash"; $vegetables[2] = "cauliflower"; $vegetables[3] = "beans"; echoer($vegetables); function echoer($array) { for ($index = 0; $index < count($array); $index++){ echo "Element $index: ", $array[$index], "\n"; } } ?> Here's what you'd see: Element 0: corn Element 1: squash Element 2: cauliflower Element 3: beans To return a value from a function, you use the return statement: return (value); The parentheses are optional. For instance, this function increments the value you pass to it and returns the result: <?php function incrementer($value) { return ++$value; } ?> Here's how you could call this function and handle its return value: <?php echo incrementer(9); function incrementer($value) { return ++$value; } ?> |