note 102276 deleted from control-structures.switch by danbrown

From: Date: Sat, 05 Feb 2011 21:00:17 +0000
Subject: note 102276 deleted from control-structures.switch by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-176707@lists.php.net to get a copy of this message
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 ;) ?>

« previous php.notes (#176707) next »