A function gets information via arguments.
You can define any number of arguments.
The arguments cannot have the same name.
When invoking the function, send the arguments in the same order as declared.
A function may contain optional arguments. And you do not need to provide a value for those arguments.
When declaring the function, you need to provide a default value for optional arguments.
In case the user does not provide a value, the function will use the default one.
<?php function addNumbers($a, $b, $printResult = false) { $sum = $a + $b; /* w ww. j a v a 2 s . co m*/ if ($printResult) { echo 'The result is ' . $sum; } return $sum; } $sum1 = addNumbers(1, 2); $sum1 = addNumbers(3, 4, false); $sum1 = addNumbers(5, 6, true); // it will print the result ?>
Here, the function takes two mandatory arguments and an optional one.
The default value of the optional argument is false, and it is used inside the function.