Re: [PHP4BETA] The String "0"

From: Date: Thu, 06 Apr 2000 23:45:45 +0000
Subject: Re: [PHP4BETA] The String "0"
References: 1  Groups: php.version4 
Request: Send a blank email to php-version4+get-13262@lists.php.net to get a copy of this message
Andreas Pour writes: > > > Hello, > > Wondering why now the string "0" equals false. Used to be you could do > something like: > > while ($entry = readdir($dp)) > { > ... > } > > > but now you have to do: > > > while (($entry = readdir($dp)) || ($entry == "0")) > { > ... > } > > which I think is going to cause a lot of people a lot of headaches. > > > Not that it's particularly hard to deal with once you figure out it's > required, but it's a strange language construct. Just curious if maybe > it's a bug. > > > Ciao, > > Andreas I don't recall at the moment whether "0" has ever meant true in PHP4, (I don't think it has) but it's been false in PHP3 since release candidate 5 (May 1998). Anyway, this issue was discussed by the core team back then; the main request for comments from the list can be found here: http://marc.theaimsgroup.com/?l=php-dev&m=90222483131959&w=2 Apparently, they decided to make the change. :) PHP4 RC2 should present an easier way to cope with this kind of situation, though: there are now '===' and '!==' operators, which test not only value equality, but also type equality. So to satisfy ===, both operands must be equal and of the same type, and the inverse for !==. So you could write the above code in a more C-like form: <?php $dh = opendir( '.' ); while ( ( $dir_entry = readdir( $dh ) ) !== false ) { echo "$dir_entry\n"; } ?> This correctly deals with the end of the directory listing, and the presence of files named '0'. But as of RC1 only === is available; !== is in cvs and should be in the next release. Cheers, Torben -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.version4 (#13262) next »