Re: PHP 4.0 Bug #8163 Updated: The string "0" tests true as empty()

From: Date: Fri, 08 Dec 2000 15:15:42 +0000
Subject: Re: PHP 4.0 Bug #8163 Updated: The string "0" tests true as empty()
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-40563@lists.php.net to get a copy of this message
i ran into that unintutive behaver and wrote this function, i dont use the empty function because of what you state, when i check data comming back from a form and its a required field and zero is a valid value for that field empty suxs, to me its not a bug because its documented to work that way, to me the function name should be "iszero" not "empty". i dont see a real use for the empty function because theres no diff between if($Var) { or if(empty($Var)){ function isblank($v) { if(!isset($v))return(1); if((gettype($v)=="string") and ($v==""))return(1); return(0); } // func hholzgra@php.net wrote: > > ID: 8163 > Updated by: hholzgra > Reported By: juhl@eisenstein.dk > Status: Bogus > Bug Type: Scripting Engine problem > Assigned To: > Comments: > > see also > > http://php.net/version4/incompatibilities.php > > [...] The String "0" is now considered empty [...] > > Previous Comments: > --------------------------------------------------------------------------- > > [2000-12-08 05:31:59] zak@php.net > empty() is used to test if a variable is set and contains a non-empty, non-zero value. The > behaviour you describe is expected and desired. > > --------------------------------------------------------------------------- > > [2000-12-08 05:24:52] juhl@eisenstein.dk > > The script below produce the following oputput: > > a is of type string > b is of type string > b is empty > > I believe the 'b is empty' part is wrong, as the variable actually contains a string > that is one char long and as such is not empty. > > <?php > $a = '1'; > $b = '0'; > > echo 'a is of type '.gettype($a).'<br>'; > echo 'b is of type '.gettype($b).'<br>'; > if (empty($a)) { > echo 'a is empty<br>'; > } > if (empty($b)) { > echo 'b is empty<br>'; > } > <snip> ..... </snip> My opinions are my own and not that of my employer even if I am self employed

« previous php.dev (#40563) next »