Hi.
On Tuesday 15 Feb 2011 at 23:41 Andre Polykanine wrote:
> Give it a default (possible empty) value:
>
> function MyFunction($x, $y, $z="") {
> // function goes here
> if (!empty($z)) {
> // The optional parameter is given
> }
> }
Using an empty string and the empty() function in this way can lead to subtle
and hard to find bugs - for example if $z = 0, the code will not be executed.
Note the list of things that are considered empty:
http://uk.php.net/manual/en/function.empty.php
Instead, consider setting the default value for $z to boolean false:
function MyFunction ($x, $y, $z = FALSE) {
if ($z) {
// do stuff with $z
}
}
In this way almost any value in $z will trigger the conditional code,
including 0 or an empty string. The exceptions are FALSE and NULL. If you
explicitly need to react to a NULL value, use is_null() to detect it.
Cheers,
Mark
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php