Specifications

$c = 1.9;
larger($a, $b);
larger($c, $a);
larger($d, $a);
will be as follows:
2.5
1.9
this function requires two numbers
Returning Values from Functions
Exiting from a function is not the only reason to use return. Many functions use return state-
ments to communicate with the code that called them. Rather than echoing the result of the
comparison in our larger() function, our function might have been more useful if we returned
the answer. This way, the code that called the function can choose if and how to display or use
it. The equivalent built-in function max() behaves in this way.
We can write our larger() function as follows:
function larger ($x, $y)
{
if (!isset($x)||!isset($y))
return -1.7E+308;
else if ($x>=$y)
return $x;
else
return $y;
}
Here we are returning the larger of the two values passed in. We will return an obviously dif-
ferent number in the case of an error. If one of the numbers is missing, we can return nothing
or return 1.7×10
308
. This is a very small number and unlikely to be confused with a real
answer. The built-in function
max() returns nothing if both variables are not set, and if only
one was set, returns that one.
Reusing Code and Writing Functions
C
HAPTER 5
5
REUSING CODE
AND
WRITING
FUNCTIONS
141
Why did we choose the number 1.7×10
308
? Many languages have defined minimum
and maximum values for numbers. Unfortunately, PHP does not. The number 1.7×10
308
is the smallest number supported by PHP version 4.0, but if this type of behavior is
important to you, you should bear in mind that this limit cannot be guaranteed to
remain the same in future. Because the present size limit is based on the underlying C
data type double, it can potentially vary between operating systems or compilers.
NOTE
07 7842 CH05 3/6/01 3:35 PM Page 141