Bug #417 [Csd]: silent (?) change in variable's value behaviour
| From: | requinix@php.net | Date: | Fri, 31 Mar 2017 09:56:41 +0000 |
| Subject: | Bug #417 [Csd]: silent (?) change in variable's value behaviour | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-208214@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=417&edit=1
ID: 417
Updated by: requinix@php.net
Reported by: jan at nrw dot net
Summary: silent (?) change in variable's value behaviour
Status: Closed
Type: Bug
-Package: Misbehaving function
+Package: *General Issues
Operating System: Linux 2.0.33, BSDI 3.1
PHP Version: 3.0 Release Candidate 5
Assigned To: zeev
-Block user comment: No
+Block user comment: Yes
Private report: N
New Comment:
@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.
Previous Comments:
------------------------------------------------------------------------
[2017-03-31 09:54:20] 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:52:08] 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:51:33] 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:05:21] 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
http://php.net/manual/en/function.empty.php
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.
https://pastebin.com/gjJwHe4T
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
------------------------------------------------------------------------
[1998-05-29 18:20:11] rasmus
Fixed by Zeev
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=417
--
Edit this bug report at https://bugs.php.net/bug.php?id=417&edit=1