Re: why is (intval('444-44444') == '444-44444') EQUAL??!
| From: | Mike Mackintosh | Date: | Fri, 22 Jun 2012 03:45:40 +0000 |
| Subject: | Re: why is (intval('444-44444') == '444-44444') EQUAL??! | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-318286@lists.php.net to get a copy of this message | ||
Using == will compare the two values after type juggling is performed. === will compare based on
value and type (identical).
PHP Will type juggle the string to an integer.
Your if/else is just like saying:
php> if (444 == "444") echo 'equal'; else echo 'not equal';
equal
--
Mike Mackintosh
PHP 5.3 ZCE
On Thursday, June 21, 2012 at 10:27 PM, Daevid Vincent wrote:
> Huh? Why is this equal??!
>
> php > $id = '444-44444';
>
> php > var_dump($id, intval($id));
> string(9) "444-44444"
> int(444)
>
> php > if (intval($id) == $id) echo 'equal'; else echo 'not equal';
> equal
>
> or in other words:
>
> php > if (intval('444-44444') == '444-44444') echo 'equal';
else
> echo 'not equal';
> equal
>
> I would expect PHP to be evaluating string "444-44444" against integer
"444"
> (or string either way)
>
> however, just for giggles, using === works...
>
> php > if ($id === intval($id)) echo 'equal'; else echo 'not equal';
> not equal
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>