php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #50809 empty(), isset() not helpful - need better alternative
Submitted: 2010-01-20 20:15 UTC Modified: 2010-11-24 09:55 UTC
From: seanr at webolutionary dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5.3.2RC1 OS: all
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2010-01-20 20:15 UTC] seanr at webolutionary dot com
Description:
------------
If the value is a string of "0", than it's not a true zero right?  The 
behavior of empty() in this case is utterly absurd.  If you can't fix 
empty, there needs to be a new function to provide a better way to 
test on this than writing our own special functions to work around a 
PHP bug.

$a = 0;
$b = '';
$b = '0'

empty($a) returns true (correctly, but not helpfully)
empty($b) returns true (correctly)
empty($a) returns true (incorrectly, since it actually does have a 
value)

Of course, isset() returns true for all three (correctly, but not 
helpfully).  This means there's no way to find out that $a and $c have 
values I can use and $b doesn't without writing my own special 
function or if statement.  EXTREMELY frustrating.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-20 21:47 UTC] degeberg@php.net
Anything that when cast to boolean is false is regarded as empty.

Seeing as false==0 and 0=="0" then false=="0" because equivalence is transitive. So if false is empty, then "0" is empty because false=="0".

If you want to check that a string's length is non-zero you can use strlen().
 [2010-01-21 12:28 UTC] colder@php.net
equivalence is not transitive: var_dump("a" == 0, 0 == "b", "a" == "b").

empty is simply awkwardly named. empty($v) is simply equivalent to 
!isset($v) || !$v.
 [2010-11-24 09:55 UTC] jani@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2010-11-24 09:55 UTC] jani@php.net
See the comments.
 [2010-11-24 09:55 UTC] jani@php.net
-Package: *General Issues +Package: Scripting Engine problem
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 17:01:30 2024 UTC