Bug #417 [Com]: silent (?) change in variable's value behaviour

[email protected] ("hugo at domibay dot es")
Newsgroups php.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=417&edit=1

 ID:                 417
 Comment by:         hugo at domibay dot es
 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:

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


Previous Comments:
------------------------------------------------------------------------
[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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.