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]

 



2007. 08. 10, péntek keltezéssel 02.31-kor Jan Reiter ezt írta:
> Hi! 
> Thank you for your response! 
> 
> The only intention of my code was to investigate the (back then) unexpected
> behavior of the if statement. 
> 
> 
> With $var['test'] set to "blah" this expression should be false
> ($var['test'] == 0) for what I know ...
> 
> $var['test'] = "blah";
> var_dump($var['test'] == 0); 
> 
> //returns bool(true)
> 
> Now I know why this happens! According to Table 6.5 of the Operators page in
> the PHP Manual in this comparison all of the values are converted to
> integer. And  atoi("blah") for sure will fail!;-) So you have to use === to
> keep the types of the values! 

I found something interesting:

znemeth@devguy3:~ php -r '$var = 'bla'; var_dump('0' == $var);'
bool(true)
znemeth@devguy3:~ php -r '$var = 'bla'; var_dump("0" == $var);'
bool(false)

version is 5.2.1

is this expected behaviour to be a difference between the two types of
quotes in this case?

greets
Zoltán Németh

> 
> Jan
> 
> 
> 
> -----Original Message-----
> From: Jim Lucas [mailto:lists@xxxxxxxxx] 
> Sent: Friday, August 10, 2007 1:47 AM
> To: Jan Reiter
> Cc: pmcurry@xxxxxxxxx; php-general@xxxxxxxxxxxxx
> Subject: Re:  I know this is not easy and I'm not stupid but...
> 
> Jan Reiter wrote:
> > Hi!
> > 
> > Phil:
> > Still I am curious what var_dump($userValues['afterDark']); at line 102.5
> > would return. 
> > I managed to recreate that fault with 
> > 
> > $var['test'] = "blah";
> > 
> > echo ($var['test']);
> > 
> > if( $var['test'] == 0)
> > {
> > 	echo "ok";
> > }
> > 
> > //this returns blahok -- not expected. 
> > 
> > In my case Var_dump() returns string(4) "blah" as expected.
> > 
> > Using 
> > 
> > if( $var['test'] === 0)
> > 
> > behaves as expected!!
> 
> Are you wanting to only test for a empty/non-empty string?
> 
> if so, use this.
> 
> if ( empty($var['test']) ) {
> 	echo "var['test'] is empty";
> }
> 
> > 
> > 
> > Jim:
> > TypeCasting would only be effective if you used the type sensitive
> > comparison operator === , because with "==" 0 equals NULL equals false
> > equals "" and so on ... or do I miss something here??
> > 
> > 
> > Hope that solves it for you! I'm still investigating why my first examples
> > fails. I've got the strong feeling that I'm missing something there. I
> don't
> > believe in a php bug or a memory leak in this case! Must be something
> pretty
> > obvious! Anyone a clue??
> > 
> > Thanks,
> > Jan
> > 
> >
> 

-- 
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