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

From: Date: Fri, 10 Aug 2007 09:20:08 +0000
Subject: RE: [PHP] I know this is not easy and I'm not stupid but...
References: 1 2 3 4 5 6 7 8  Groups: php.general 
Request: Send a blank email to php-general+get-260554@lists.php.net to get a copy of this message
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@cmsws.com] > Sent: Friday, August 10, 2007 1:47 AM > To: Jan Reiter > Cc: pmcurry@gmail.com; php-general@lists.php.net > Subject: Re: [PHP] 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 > > > > >

« previous php.general (#260554) next »