[Previous] [Contents] [Next]


Argument Types and Return Types

The argument and return types of a function aren't declared when the function is defined. PHP allows arguments of any type to be passed to the function, and as with variables, the return type is determined when a result is actually returned. Consider a simple function that divides two numbers:

function divide($a, $b)
{
    return ($a/$b);
}

$c = divide(4, 2);  // assigns an integer value = 2
$c = divide(3, 2);  // assigns a float value = 1.5
$c = divide(4.0, 2.0); // assigns a float value = 2.0

If the types of arguments passed to the function are critical, they should be tested as shown earlier in Section 2.1.3.


[Previous] [Contents] [Next]