Re: = 0 and = "0"
| From: | Robin Chen | Date: | Thu, 25 Oct 2001 08:59:41 +0000 |
| Subject: | Re: = 0 and = "0" | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-72220@lists.php.net to get a copy of this message | ||
Thank you, that was it. I needed to test the variable against both an
integer and a string, so I ended up using (string) when I need to
compare a string. Rest of the time, it's an integer.
Robin
Rasmus Lerdorf wrote:
>
> > If you set $qty=0; then $qty has no value.
>
> Of course it has a value. The value is 0.
> Quite distinct from not having a value, or in proper terms, not being set.
> Try this:
>
> var_dump($qty);
> $qty = 0;
> var_dump($qty);
>
> Well, I will save you the trouble, it outputs:
>
> NULL
> int(0)
>
> > Type casting is irrelevant in php.
>
> No they aren't. Try this:
>
> $qty = 0;
> if($qty=="test") echo "foo";
> if((string)$qty=="test") echo "bar";
>
> This will only output "foo". Not "bar". You could also use the ===
> operator to force PHP to check not only the value but also the type.
>
> See
> http://www.php.net/manual/en/language.operators.comparison.php
>
> -Rasmus
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net