Bug #417 [Com]: silent (?) change in variable's value behaviour
[email protected] ("spam2 at rhsoft dot net")
| Newsgroups | php.bugs |
|---|---|
| Message-ID | <[email protected]> |
Edit report at https://bugs.php.net/bug.php?id=417&edit=1 ID: 417 Comment by: spam2 at rhsoft dot net Reported by: jan at nrw dot net Summary: silent (?) change in variable's value behaviour Status: Closed Type: Bug Package: Misbehaving function Operating System: Linux 2.0.33, BSDI 3.1 PHP Version: 3.0 Release Candidate 5 Assigned To: zeev Block user comment: N Private report: N New Comment: 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 Previous Comments: ------------------------------------------------------------------------ [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 ------------------------------------------------------------------------ [1998-05-29 17:54:01] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=417&edit=1