Web site counters - 109Chapter 6Control and FunctionsAlternatively, if the only way
109Chapter 6Control and FunctionsAlternatively, if the only way we ever use these price comparisons is to print which deal ispreferred, we can include the printing in the function, like this: function print_better_deal ($amount_1, $price_1, $amount_2, $price_2) { $per_amount_1 = $price_1 / $amount_1; $per_amount_2 = $price_2 / $amount_2; if ($per_amount_1 < $per_amount_2) print( The first deal is better!
); elseprint( The second deal is better!
); } $liters_1 = 1.0; $price_1 = 1.59; $liters_2 = 1.5; $price_2 = 2.09; print_better_deal($liters_1, $price_1, $liters_2, $price_2); Our first function used the returnstatement to send back a Boolean result, which was usedin an iftest. The second function has no return statement, because it is used for the sideeffect of printing text to the user s browser. When the last statement of this function is exe- cuted, PHP simply moves on to executing the next statement after a function call. Formal parameters versus actual parametersIn the preceding examples, the arguments we passed to our functions happened to be vari- ables, but this is not a requirement. The actual parameters (that is, the arguments in the function call) may be any expression that evaluates to a value. In our examples, we couldhave passed numbers to our function calls rather than variables, as in: print_better_deal(1.0, 1.59, 1.5, 2.09); Also, notice that in the examples we had a couple of cases where the actual parameter vari- able had the same name as the formal parameter (for example, $price_1), and we also hadcases where the actual and formal names were different. ($liters_1is not the same as$amount_1.) As we will see in the next section, this name agreement doesn t matter eitherway the names of a function s formal parameters are completely independent of the worldoutside the function, including the function call itself. Argument number mismatchesWhat happens if you call a function with fewer arguments than appear in the definition, orwith more? As you might have come to expect by now, PHP handles this without anythingcrashing, but it may print a warning depending on your settings for error reporting. Too few argumentsIf you supply fewer actual parameters than formal parameters, PHP will treat the unfilled for- mal parameters as if they were unbound variables. However, under the usual settings forerror reporting in PHP5, you will also see a warning printed to the browser.
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.