RE: I know this is not easy and I'm not stupid but...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ummh, no!

(int) and (integer) perform a C style atoi() conversion. 

(int)"blah" => integer 0
(int)""	=> integer 0
(int)"1"	=> integer 1
(int)"12"	=> integer 12

 ("1" == 0)  	=> false
 ("blah" == 0) 	=> true

( (int)$userValues['afterDark'] === 0 ) only is true, if
$userValues['afterDark'] is a string not containing an integer. This is what
happens:

$userValues['afterDark'] gets converted to integer by atoi(), and after that
is compared by type and value to integer 0. So the type part of the test
will pass for sure. 

But perhaps you should try to adapt the comparison to the DB value. So try
($userValues['afterDark'] == (string)0)
This expression is only true if $userValues['afterDark'] equals "0", not
when $userValues['afterDark'] equals "" or "blah" or "1"

Hope that helps!

Jan


-----Original Message-----
From: Phil Curry [mailto:pmcurry@xxxxxxxxx] 
Sent: Friday, August 10, 2007 2:49 AM
To: Stut
Cc: php-general
Subject: Re:  I know this is not easy and I'm not stupid but...

> Please include the list when replying.
>
> Phil Curry wrote:
>>> Phil Curry wrote:
>>>> how can this be? This is not the first time I've run into a  
>>>> situation like this. What am I missing?
>>>> line 102    echo  ($userValues['afterDark']);             //  
>>>> outputs 1
>>>> line 103    if ( $userValues['afterDark'] == 0 ) {        // passes
>>>
>>> Add a line at 102.5...
>>>
>>> var_dump($userValues['afterDark']);
>>>
>>> What type is that variable?
>> Don't have to do a dump, I know its a tinyint(1), not null, default 0
>
> That would be the type in the database, not the type of that  
> variable at that time.
>
> -Stut
>
> -- 
> http://stut.net/

Absolutely right. didn't think of that.

var_dump($userValues['afterDark']);  ==> string(1) "1"

but then I'm comparing a string to an int and the result always seems  
to pass. So does that mean any string compared to any int will be true?
By casting (int)$userValues['afterDark'] the test still passes and  
var_dump shows the (int)$userValues['afterDark'] as an int(1) but  
does give a value like the previous example.
ie. string(1)"1" but only int(1) Not sure if the expression has a  
value of "1" now that its been cast.

Then finally  if ( (int)$userValues['afterDark'] === 0 ) {        //  
passes

So:
if ( $userValues['afterDark'] == 0 ) {                  // passes
if ( (int)$userValues['afterDark'] == 0 ) {           // passes
if ( (int)$userValues['afterDark'] === 0 ) {         // passes

And the value of afterdark in the mysql table is tinyint(1) 1.
And echo( 1+(int)$userValues['afterDark']);    // outputs 2

Now I'm confused!
-Phil

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux