php-general Digest 22 Jun 2012 21:07:28 -0000 Issue 7864

Topics (messages 318285 through 318288):

Re: why is (intval('444-44444') == '444-44444') EQUAL??!
        318285 by: Robert Cummings
        318286 by: Mike Mackintosh
        318287 by: marco.behnke.biz

If PHP Were British
        318288 by: Daevid Vincent

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---


On 12-06-21 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

Using === will always fail because on the left you have a string and on the right you have an integer which fails exact comparison based on datatype mismatch.

When comparing a string to an integer using == PHP performs type juggling and converts the string to an integer first.

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.

--- End Message ---
--- Begin 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
> 
> 



--- End Message ---
--- Begin Message ---

Daevid Vincent <dae...@daevid.com> hat am 22. Juni 2012 um 04:27 geschrieben:

> Huh? Why is this equal??!

http://de2.php.net/manual/en/language.types.type-juggling.php

--- End Message ---
--- Begin Message ---
http://www.addedbytes.com/blog/if-php-were-british/

--- End Message ---

Reply via email to