php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19056 empty() function is completely broken
Submitted: 2002-08-22 15:47 UTC Modified: 2002-08-23 12:25 UTC
From: adets at idsk dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 4.2.2 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: adets at idsk dot com
New email:
PHP Version: OS:

 

 [2002-08-22 15:47 UTC] adets at idsk dot com
empty() function is not working correctly.    
It returns true if variable has a value of: 0 & 0.0    
(integer, float), false (boolean) or "0" (string).    
    
This makes this function COMPLETELY USELESS.  
   
Is it possible to implement this function correctly (it 
will be great!!!), remove it at   
all (because it is useless now) or at least correct 
documentation?   

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-08-22 15:52 UTC] rasmus@php.net
The example in the documentation shows clearly that empty() returns true when a var is not set or when it is set to something that evaluates to 0.  If you only want to check whether something is set, use the isset() function.
 [2002-08-22 16:08 UTC] adets at idsk dot com
I don't want to check that is is set (we have isset() 
fucntion as you noted before), I want to check that it is 
_not_ empty. 
If you'll look in the user comments to the empty() function 
than you'll find that this is a very common problem - 
usually it has no sence to check that smth. is _not_ zero 
(because it is more easy to compare it with zero), the task 
is to check that it is _not_ _empty_, for example, if 
somebody submits a form it makes a real difference - "" 
string (_empty_, user passed empty field) and "0" string 
(that _has_ a value). 
Now people are using additional checks just because of the 
fact that empty() function is not working where it should.
 [2002-08-22 16:12 UTC] rasmus@php.net
Well, it has always been documented to work like this and it isn't going to change as it would break countless applications.  And it is only "useless" for fields where "0" is valid input.  For those use isset()&&strlen().  Basically empty() works the same as just doing if($var) and if("0") is still false, so any other behaviour would be inconsistent.
 [2002-08-23 12:23 UTC] adets at idsk dot com
Yes, breaking of compatibility is a very bad thing.   
But may be at least for PHP-5 it can be possible to change   
empty() behaviour to not return true for "0" _string_?   
Seems that PHP can distinguish data types... Such change 
can make code much more clean... 
Ok, let it be a feature request from an old PHP user ;-)
 [2002-08-23 12:25 UTC] rasmus@php.net
No, all data coming across an HTTP request end up as strings.  "0" and 0 are the same thing.  I don't see empty() changing.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 29 16:01:30 2024 UTC