Note Submitter: jekyl1982
----
Problem with (int)0 in switch statement
<?php
// param
$param = 1;
// should be bool(true)
var_dump(preg_match('/[0-9]/', $param) === 1);
echo "<BR>";
// should be bool(false)
var_dump(preg_match('/[0-9]/', $param) === 0);
echo "<BR>";
// should be bool(false)
var_dump(preg_match('/[a-z]/', $param) === 1);
echo "<BR>";
// now some kind of black magic
// for $param = 0 it will print STRING but for $param = 1 it will print INT
switch ($param) {
case (preg_match('/[0-9]/', $param) === 1):
echo 'INT';
break;
case (preg_match('/[a-z]/', $param) === 1):
echo 'STRING';
break;
default:
echo 'nothing';
}
// good match, in switch put (bool)TRUE
switch (TRUE) {
case (preg_match('/[0-9]/', $param) === 1):
echo 'INT';
break;
case (preg_match('/[a-z]/', $param) === 1):
echo 'STRING';
break;
default:
echo 'nothing';
}
// now it will print INT
// isn't it strange have fun with PHP ;)
?>
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.