Re: Custom function
| From: | Andre Polykanine | Date: | Wed, 16 Feb 2011 00:46:48 +0000 |
| Subject: | Re: Custom function | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-311349@lists.php.net to get a copy of this message | ||
Hello Mark,
Hm... will
if ($z)
evaluate to true if $z==0?
I thought no...
Actually, we can use
if (isset($z))
--
With best regards from Ukraine,
Andre
Skype: Francophile
My blog: http://oire.org/menelion (mostly in Russian)
Twitter: http://twitter.com/m_elensule
Facebook: http://facebook.com/menelion
------------ Original message ------------
From: Mark Kelly <php@wastedtimes.net>
To: php-general@lists.php.net
Date created: , 2:21:36 AM
Subject: [PHP] Custom function
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