php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44999 switch statement uses equivalence
Submitted: 2008-05-14 21:51 UTC Modified: 2013-09-16 14:49 UTC
From: ethan dot nelson at ltd dot org Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.6 OS: windows 2003
Private report: No CVE-ID: None
 [2008-05-14 21:51 UTC] ethan dot nelson at ltd dot org
Description:
------------
Right now, 0 compared to any string will prove true.

Reproduce code:
---------------
<?php

if (0 == 'anystring') echo "True";
else echo "False";

?>

Expected result:
----------------
False

Actual result:
--------------
True

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-14 22:20 UTC] cellog@php.net
see Bug #44990 for explanation
 [2008-05-14 22:25 UTC] ethan dot nelson at ltd dot org
I don't recall PHP ever behaving that way.

So how do you alter a switch statement to use the identical operator instead of equivalency?

It seems somewhat odd that any text character would be equvalent to integer 0.  As I remember the chart it was something like:
0 == '0' true
0 == '' true
0 == null true
0 == false true
0 == 'text' false

Basically if we are going to treat any string as equivalent to 0, the switch statement becomes useless when iterating through arrays unless you first strip key names of integer 0.
 [2012-08-24 08:16 UTC] bugs dot php dot net at simoneast dot net
This is quite ridiculous.  Can this 'feature' of PHP *please* be reconsidered?
 [2013-02-14 16:49 UTC] radamanf at gmail dot com
I can see that it's no any logical explanation to keep this BUG unfixed! Status: Not a bug -> why??? It's not funny, so many years past.

I do love to use PHP and like that no needed to define variable types, but this is generic FAIL.

Come one guys, someone need to fix this, please.

My PHP version
PHP 5.3.10-1ubuntu3.5 with Suhosin-Patch (cli) (built: Jan 18 2013 23:45:59)
 [2013-04-21 21:44 UTC] contact at caus909 dot net
Maybe because that's not a bug... "It's not a bug, it's a feature".
Equality (==) and identity (===) are different, if for your use, equality is the 
best, just use it then.
 [2013-09-15 03:58 UTC] eric dot alex dot dube at gmail dot com
I see this as the biggest inconsistency:
var_dump(0 == "uno"); // true
var_dump(0 == false); // true
var_dump(false == "uno"); // false

While perhaps not a bug by strict definition, I think it's definitely an error in 
design.
 [2013-09-15 04:08 UTC] eric dot alex dot dube at gmail dot com
It seems object do behave the way one would expect strings to (furthering the 
inconsistency imo), as demonstrated by this example:

class Tester {
}
$tester = new Tester();

var_dump(0 == $tester); // false
echo "<br />";
var_dump(0 == false); // true
echo "<br />";
var_dump(false == $tester); // false
echo "<br />";
 [2013-09-16 14:49 UTC] ethan dot nelson at ltd dot org
-Summary: 0 equals any string +Summary: switch statement uses equivalence
 [2013-09-16 14:49 UTC] ethan dot nelson at ltd dot org
This is not about equivalency vs identical operators.  They have defined behavior working according to definition.

This is about the fact that the switch statement works using equivalency and there is no way to push it to use identical instead.  This leads to unpredictable results when you would expect a default case to execute but instead a "case 0" executes.  This is due to so many things proving "equivalent" to zero, including null.

There should be a way to push switch to use identical or a "switchs" statement for switch strict.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 18:01:29 2024 UTC