Functions Revisited

Now that we have covered arrays, we can examine some built-in functions that could help you make your own functions more flexible. If you have programmed in Perl before, you will know that you can easily create subroutines that accept a variable number of arguments. PHP provides functions that make it just as easy.

Imagine that you have created a function that accepts three string arguments and returns a single string containing each of the provided arguments wrapped in an HTML table, which includes the sum of the numbers in its final row:

 function addNums( $num1, $num2 ) { $result = $num1 + $num2; $ret = "<table border=\"1\">"; $ret .= "<tr><td>number 1: </td><td>$num1 </td></tr>"; $ret .= "<tr><td>number 2: </td><td>$num2 ...

Get Sams Teach Yourself PHP in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.