Re: is_null() and is_string() reversed when using in switch case statements...
| From: | Simon J Welsh | Date: | Fri, 15 Jul 2011 02:29:21 +0000 |
| Subject: | Re: is_null() and is_string() reversed when using in switch case statements... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-314087@lists.php.net to get a copy of this message | ||
On 15/07/2011, at 1:58 PM, Daevid Vincent wrote:
> function test($v)
> {
> var_dump($v);
>
> if (is_string($v)) echo "FOUND A STRING.\n";
> if (is_null($v)) echo "FOUND A NULL.\n";
>
> switch ($v)
> {
> case is_string($v):
> echo "I think v is a string, so this is broken.\n";
> break;
> case is_null($v):
> echo "I think v is null, so this is broken.\n";
> break;
> case is_object($v):
> $v = '{CLASS::'.get_class($v).'}';
> echo "I think v is a class $v\n";
> break;
>
> case is_bool($v):
> $v = ($v) ? '{TRUE}' : '{FALSE}';
> echo "I think v is boolean $v\n";
> break;
> case is_array($v):
> $v = '['.implode(',',$v).']';
> echo "I think v is an array $v\n";
> break;
> case is_numeric($v):
> echo "I think v is a number $v\n";
> break;
> }
> }
In both cases, $v is equivalent to false, so is_string(NULL) = false == NULL == $v, likewise for
is_null($v);
You're most likely after switch(true) { … } rather than switch($v)
---
Simon Welsh
Admin of http://simon.geek.nz/