ID:               50878
 Updated by:       [email protected]
 Reported By:      stephan dot schulze at kapthon dot com
-Status:           Open
+Status:           Bogus
 Bug Type:         Scripting Engine problem
 Operating System: CentOs 5.4
 PHP Version:      5.3.1
 New Comment:

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

Switch uses loose comparison, so using a boolean true as the expression
to be switched by will match any non-zero case.

Case in point:

<?php
$f = true;
switch ($f) {
    case -1:
        echo "matched\n";
        break;

    default:
        echo "default\n";
}
?>

This prints "matched", since -1 == true. This is documented (in some
detail) at
http://au2.php.net/manual/en/types.comparisons.php#types.comparisions-loose


Previous Comments:
------------------------------------------------------------------------

[2010-01-29 08:29:34] stephan dot schulze at kapthon dot com

Description:
------------
If a class function returns a class constant or true and the result is
checked with a switch, the switch does not enter the correct block.
I expect $bResult should be converted to 1 if it is "true" but not to
-1.

Reproduce code:
---------------
<?php
class Foo {
   const ERR_ONE = -1;
   
   public function bar() {
      return true;
   }
   
   public function isErr() {
      return self::ERR_ONE;
   }
}
$oFoo = new Foo();
$bResult = $oFoo->bar();
switch ($bResult) {
   case Foo::ERR_ONE:
      "ERR_ONE: " . var_dump($bResult);      
   break;  
   default:
      echo "default";
   break; 
}

$bResult = $oFoo->isErr();
switch ($bResult) {
   case Foo::ERR_ONE:
      "ERR_ONE: " . var_dump($bResult);      
   break;  
   default:
      echo "default";
   break;  
}

Expected result:
----------------
Expected to get
"default" and "ERR_ONE: int(-1)"

Actual result:
--------------
ERR_ONE: bool(true) ERR_ONE: int(-1) 


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=50878&edit=1

Reply via email to