php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46004 A non stict condition with 0 return true
Submitted: 2008-09-05 15:33 UTC Modified: 2008-09-05 17:11 UTC
From: roland dot dufour at multiprog dot net Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.6 OS: Windows Vista Ultimate French
Private report: No CVE-ID: None
 [2008-09-05 15:33 UTC] roland dot dufour at multiprog dot net
Description:
------------
When I try a non strict condition 0 == 'test', PHP return true.

Reproduce code:
---------------
// Non-strict condition 
var_dump((0 == 'test'));
var_dump((1 == 'test'));
var_dump(('test' == 'test'));

// Strict condition 
var_dump((0 === 'test'));
var_dump((1 === 'test'));
var_dump(('test' === 'test'));

Expected result:
----------------
bool(false) bool(false) bool(true) 

I don't understand why a condition 0 == 'test' return true.
I discovered this "bug" by exploring the keys of a table via the instruction "foreach". My code was then:

$options = array(
	array(), // an array
	array(), // an other array
	'refProduct' => 'test' // a value
);
foreach ($options as $k => $option) {
	if ('refProduct' == $k){
		continue;
	}
	// Instructions...
}

Curiously, only my secondary table ("an other array") was executed by "Instructions...".

Actual result:
--------------
bool(true) bool(false) bool(true) 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-05 15:36 UTC] roland dot dufour at multiprog dot net
With a non strict condition, the expected result is :
bool(true) bool(false) bool(true)

And, with a strict condition, the expected result is :
bool(false) bool(false) bool(true)
 [2008-09-05 17:11 UTC] tularis@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Please read the documentation:
http://www.php.net/manual/en/types.comparisons.php

It clearly states:
(int) 0 == (string) "php"
(or any string for that matter), this is due to PHP's loose typing auto-converting both to integers (and "php" changes to (int) 0; 0 == 0, so true)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 10:01:30 2025 UTC