PHP 5.2.12 - Interesting Switch Statement Bug With Integers and Strings
Posted
by Levi Hackwith
on Stack Overflow
See other posts from Stack Overflow
or by Levi Hackwith
Published on 2010-04-10T01:57:38Z
Indexed on
2010/04/10
2:03 UTC
Read the original article
Hit count: 373
php
<?php
$var = 0;
switch($var) {
case "a":
echo "I think var is a";
break;
case "b":
echo "I think var is b";
break;
case "c":
echo "I think var is c";
break;
default:
echo "I know var is $var";
break;
}
?>
Maybe someone else will find this fascinating and have an answer. If you run this, it outputs I think the var is a
when clearly it's 0
. Now, I'm most certain this has something to do with the fact that we're using strings in our switch
statement but the variable we're checking is an integer. Does anyone know why PHP behaves this way? It's nothing too major, but it did give me a bit of a headache today.
Thanks folks!
© Stack Overflow or respective owner