php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #23110 Ugly behaviour of == (and ===)
Submitted: 2003-04-08 06:43 UTC Modified: 2004-08-06 21:13 UTC
Votes:7
Avg. Score:4.9 ± 0.3
Reproduced:5 of 6 (83.3%)
Same Version:2 (40.0%)
Same OS:4 (80.0%)
From: kyrael at web dot de Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.1 OS: Linux
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: kyrael at web dot de
New email:
PHP Version: OS:

 

 [2003-04-08 06:43 UTC] kyrael at web dot de
<?php
list($var1,$var2)=array("1e1","10");
var_dump($var1);var_dump($var2);
var_dump($var1==$var2);
var_dump($var1===$var2);
?>
string(3) "1e1"
string(2) "10"
bool(true)
bool(false)

This behaviour is ugly - both operands are strings, and they are clearly not equal. '1e0' and '1.0' and '1' are "equal" too. Same with '3.20' and '3.2'. Does PHP think i am stupid and unable to type numbers when i want them?
According to the manual, == returns true if the two operands are equal, and === returns true if the operands are equal and of the same type. == says they are equal, and according to var_dump they are both strings - why doesn't === return true then? So this behaviour is neither logical nor documented. String comparison, even with == when both operands are strings, should return 'equal' if and only if the strings are really equal. Wouldn't be PHP5 a chance to change this?

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-04-08 06:57 UTC] kyrael at web dot de
Btw, in switch you can't use strcmp or ===, because switch seems to use ==.
 [2003-05-02 13:10 UTC] nicos@php.net
This will not change, the documentation team needs to be clear on that.

We don't have '===' for nothing.
 [2004-02-04 18:15 UTC] tgrey at supercomputerinc dot com
highly illogical indeed.  if the types differ, how could the values be considered equal?  when working with functions that could have different input types this behaviour is disasterous.  it seems to be counterproductive to loose variable types, since a data type check has to be added.

working around is often possible, but if i wanted to code around my *** to get to my elbow i'd be using perl.  imho this behavior needs some strong reconsideration.

very simple example:
<?
$in="blah";
if($in==0)
	echo "zero: $in";
else
	echo "not zero: $in";
?>

or worse yet:
<?
function test($in=0)
{
switch($in)
{
	case 0:
		echo "zero: $in";
		break;
	case "blah":  // never reached!
		echo "blah: $in";
		break;
	default:
		echo "default: $in";
		break;
}
}
test();
test("blah");
test(2);
?>
 [2004-05-22 11:20 UTC] nlopess@php.net
See also #20726.
 [2004-08-06 21:13 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"If you compare integer with the string, the string is converted to number. If you compare two numerical strings, they are compared as integers. These rules are valid also for the switch statement." + example:

<?php
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true

switch ("a") {
case 0:
    echo "0";
    break;
case "a": // never reached because "a" is already matched with 0
    echo "a";
    break;
}
?>

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 19:01:29 2024 UTC