php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #26821 if("NANC" < 0) - always evaluates TRUE
Submitted: 2004-01-06 16:28 UTC Modified: 2004-01-09 14:12 UTC
From: ivo at benetech dot org Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 4.3.4 OS: Linux / RH 7.3
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ivo at benetech dot org
New email:
PHP Version: OS:

 

 [2004-01-06 16:28 UTC] ivo at benetech dot org
Description:
------------
The statement...

if("NANC" < 0)

...always evaluates TRUE.

Reproduce code:
---------------
if("NANC" < 0)
{
   print("should not happen...but does\n");
}
else
{
   print("should happen...but doesnt\n");
}

Expected result:
----------------
echoed to the console:

should happen...but doesnt

Actual result:
--------------
echoed to the console:

should not happen...but does

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-06 17:23 UTC] sniper@php.net
RTFM:

http://www.php.net/manual/en/types.comparisons.php

(never ever compare different types like that)

 [2004-01-06 18:08 UTC] ivo at benetech dot org
Documentation needs to be more explicit about php being strongly typed.

http://www.php.net/manual/en/language.types.type-juggling.php

or

http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion
 [2004-01-07 05:53 UTC] vrana@php.net
"NANC" is converted to 0 for comparison. It works for me as expected. What is printed by the code <?php echo intval("NANC"); ?> on your system?
 [2004-01-07 13:20 UTC] ivo at benetech dot org
It also print 0 as expected.

if( (int) "NANC" < 0)

also evaluates as expected.
 [2004-01-08 22:35 UTC] skissane at ics dot mq dot edu dot au
Vrana: your comment makes no sense. If "NANC" is converted to 0, then 0 < 0 would be FALSE, not true.

Compare these expressions:
"NANC" < 0    result TRUE
"NANC" < "0"  result FALSE
0 < 0         result FALSE

So it seems it is neither converting "NANC" to an integer 0, nor 0 to a string "0". What actually is going on? The documentation should clarify this.
 [2004-01-09 11:04 UTC] sp@php.net
Who wants to evaluate "NANC" < 0??? It _absolutely_ makes no sense. If someone asks you if A+B > 4 is, you wouldn't know what to calculate...

FYI I've tried ivo's script. Under PHP 4.3.3 it produces "should happen...but doesnt" and under 4.3.0 it's "should not happen...but does". 


 [2004-01-09 11:32 UTC] ivo at benetech dot org
> If someone asks you if A+B > 4 is, you wouldn't know what to calculate...

I sincerely hope this would hold:

http://www.php.net/manual/en/language.operators.php#language.operators.precedence

....otherwise theres bigger problems.

If a statement such as 

"some arbitrary string" < 0

is not to be trusted then the documentation should be updated to reflect this. Currently it explains strong comparisons with '===', weak with '==' but it does not mention < or >, ony obliquely and it obviously does not.

(http://www.php.net/manual/en/types.comparisons.php)

p.s. I am working with a site that manipulates strings everywhere. most functions return < 0 on error or the processed string if all goes well. just finished updting it to return ints always & use references for strings.

still think it's an outright bug or at least a documentation problem due to all the 'weak' type comparison references & examples everywhere.
 [2004-01-09 14:12 UTC] sp@php.net
As said by sniper: never ever compare different types like that!!!

In case $var is an int, you can do a $var < 0. In case $var is  a string, PHP don't know what to do (try this with Java or C++!).

Let your called functions return FALSE on error and everything works fine. Or as an alternative, try something like 

$var=somefunction();
if(is_integer($var)&&$var<0)
{
   // Here comes the error-Processing
   print("Error\n");
}
else
{
   // Do something with $var
   print $var;
}



 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 11:01:37 2025 UTC