php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20708 'foo'==0 returns true
Submitted: 2002-11-28 18:02 UTC Modified: 2002-11-28 18:13 UTC
From: wb at sapo dot pt Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.2.3 OS: linux 2.2.20, linux 2.4.19
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: wb at sapo dot pt
New email:
PHP Version: OS:

 

 [2002-11-28 18:02 UTC] wb at sapo dot pt
sorry, im really not sure if this is a kind of bug or if i'm getting something wrong: the two lines

echo 'foo'==0 ? 'is true ' : 'is false ';
if('bar'==0) echo 'is true '; else echo 'is false ';

will output:
is true is true

i dont understand why 'foo'==0 returns true.

the way i understand the manual, php should consider 'foo' a nonempty string and 0 a zero integer. even if it would do a boolean comparison, according to the manual 'foo' should be juggled to true and 0 should be juggled to false. how can 'foo'==0 return true? have i missed something?

i hope i do not resubmit a known issue, i tried some queries but i really didnt know how to lookup this issue.

this happens with variables as well. i ran into this problem in my programming when comparing array keys that could be numeric or string. a 0 array key compared against any nonempty string array key always returned true. it took me a while to find out that's why my program didn?t work the way i thought it should. 

i hope this is not something that happens only to me, but i have tested the two lines on three different boxes before writing to you.

my configure line:
./configure' '--with-mysql=/usr/local/mysql' '--with-apache=../apache_1.3.26' '--enable-ftp' '--with-zlib-dir=/usr/lib'

Best regards,
wolfgang

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-11-28 18:11 UTC] wb at sapo dot pt
another summary
 [2002-11-28 18:13 UTC] rasmus@php.net
The numerical value of 'foo' is 0.  If you want to compare both value *and* type, use ===
 [2002-11-28 18:34 UTC] wb at sapo dot pt
thank you for very fast information.

type juggling is a great feature but really hard to learn. now i learned that it casts my string to numeric for the comparison with an integer - my coding would have needed the integer casted to a string. 

sorry for bothering you. i will go and look for more information about precedence of type casting in comparisons if it is that way it works.
 [2004-04-02 11:01 UTC] milos at mcservices dot com
This should be BOLDED in RED in documentation!
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Aug 28 01:00:03 2025 UTC