php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65551 Compare 0 variable alway return true
Submitted: 2013-08-25 16:39 UTC Modified: 2013-08-25 16:46 UTC
From: nukboon at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.5.3 OS: Centos 6
Private report: No CVE-ID: None
 [2013-08-25 16:39 UTC] nukboon at gmail dot com
Description:
------------
See below my testing script. I've test with all version of php it's return same 
result.

Test script:
---------------
$a = 0;
if($a == 'id'){
	echo 'Yes' . PHP_EOL;
}

var_dump($a);
var_dump($a == 'id');
var_dump($a == 'ids');
var_dump($a == 'def');

$a = 1;
if($a == 'id'){
	echo 'Yes' . PHP_EOL;
}

var_dump($a);
var_dump($a == 'id');
var_dump($a == 'ids');
var_dump($a == 'def');

// Result
Yes
int(0)
bool(true)
bool(true)
bool(true)
int(1)
bool(false)
bool(false)
bool(false)

Expected result:
----------------
$a = 0;
var_dump($a == 'abc');
//should be: bool(false)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-08-25 16:46 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2013-08-25 16:46 UTC] rasmus@php.net
You are mixing types, so type-juggling comes into effect. (int)'id' is 0 and thus 
0==0. Read http://php.net/juggling
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 16:01:30 2024 UTC