|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #417 silent (?) change in variable's value behaviour
Submitted: 1998-05-29 17:54 UTC Modified: 2017-03-31 09:56 UTC
From: jan at nrw dot net Assigned: zeev (profile)
Status: Closed Package: *General Issues
PHP Version: 3.0 Release Candidate 5 OS: Linux 2.0.33, BSDI 3.1
Private report: No CVE-ID: None
 [1998-05-29 17:54 UTC] jan at nrw dot net
I'm not sure if this is to be called a bug or a feature, but this is the problem i have:

- i wrote some apps in PHP3RC4, with excessive use of forms and PHP3-scripts which get form fields POSTed.
These scripts contain many value-checks like "if ($form_field1) do something;" this worked like i expected - empty 
fields in the form resulted in "False" in the above statement.
I then upgraded to PHP3RC5 - and now empty form fields result in "True". I realized the new function "empty()" 
and changed scripts to use this funktion, but now a form-input of "0" results in "empty()=True". 
This behaviour is against all i'm used to in other scripting languages, i must say. Also, i'm not sure what the 
intentional use of "empty()" is - a string or Value of "0" isn't an empty variable container for me... or am i totally wrong here?

At least, i really would like to know what the intentional behaviour of PHP3/form fields/variables/empty()/if() 
is or should be. 
Apart from that - PHP3 becomes better and better! It's a pleasure writing apps for the web with it!

thanks for any help/hint, Jan


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [1998-05-29 18:20 UTC] rasmus
Fixed by Zeev
 [2017-03-31 09:05 UTC] hugo at domibay dot es
This Behaviour still persists in PHP 5.6 on Centos7

I am running constantly into the same problem:

The Documentation states on
The Function empty() would trigger on Values like
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
as empty($var) === true

But I found in many cases a String containing a "0" is not empty. It contains a Character that is "0".
If I store the Character "0" into a file the file is not empty anymore it contains the text "0".
For example:
$ echo "0" > myfile.txt

On Integers the Value 0 can be a valid number according to the Programming Logic as an Index of an Array for example.
The same goes for Float Numbers.

I found discarding those Values as "empty" is an Error in most cases of a complex Application.

Test script: 
This Script might be simple but it leads to silent Application Errors that are hard to track down.

Actual result:
The Script will print only:
'1' => '2'
The other 2 Entries are silently rejected because of the Values "0".

Expected result:
The Script should print out:
'0' => '1'
'1' => '2'
'3' => '0'
On Strings only Values of NULL or "" should trigger empty($var) === true
On Integers only Values of NULL should trigger empty($var) === true
On Float Numbers only Values of NULL should trigger empty($var) === true
 [2017-03-31 09:51 UTC] hugo at domibay dot es
I found by the Years the PHP Programmer Community has come about with different workaround for this issue:

if(strlen($string) > 0)
  echo "string is empty\n";

but for text with non latin characters you need the mb_strlen() Function to savely check the string.

if(mb_strlen($string) > 0)
  echo "string is empty\n";

Then I also found other solutions like

if(isset($string) && $string !== "")
  echo "string is empty\n";

if(is_numeric($string) || isset($string[0]))
  echo "string is empty\n";

I found that (NULL !== "") would result as "true"
so I needed to add isset() to the check.

Then if the variable would be a Number like $string = 5 
the Check (isset($string[0])) would result as "false" because this Check does not work for numbers
so I needed to add is_numeric($string) to the check.

But adding all those additional checks to get the right Behaviour results in performance drop downs.

So that I finally got the right result with an acceptable performance with the Check

if(isset($string) && $string !== "")
  echo "string is empty\n";
 [2017-03-31 09:52 UTC] spam2 at rhsoft dot net
yeah because '0' is *not* empty when it is a string '' would be empty

hence you should cast expected integers with (int)$var and also consider the scalar type hints and return types introduced in PHP7
 [2017-03-31 09:54 UTC] hugo at domibay dot es
I have to correct my little snippet:

if(isset($string) && $string !== "")
  echo "string is not empty\n";
 [2017-03-31 09:56 UTC]
-Package: Misbehaving function +Package: *General Issues -Block user comment: No +Block user comment: Yes
 [2017-03-31 09:56 UTC]
@hugo, a 19 year old bug report against a version of PHP that nobody even thinks about anymore is hardly the appropriate place for this.
0, "0", and 0.0 are empty() and that will not change anytime soon.
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Jul 22 10:01:25 2019 UTC