note 105988 deleted from control-structures.switch by danbrown

From: Date: Fri, 30 Sep 2011 21:42:56 +0000
Subject: note 105988 deleted from control-structures.switch by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-183573@lists.php.net to get a copy of this message
Note Submitter: dpwss at hotmail dot com ---- To lchanady at gmail dot com27-Jan-2010 05:56 You wrote: "Something fairly simple (and maybe obvious) that I didn't see mentioned is that the default case WILL be executed even if the switched variable does not exist or is undefined. For example: <?php $a = "abc"; $b = "def"; switch($c){ case "a": echo "a"; break; case "b": echo "b"; break; default: echo "default"; break; } ?> Will output: default Even though $c was never declared or defined, the default case will still be executed rather than PHP throwing an error. " To PHP, $c would appear as "" so by the switch logic written above, it's not equal to "a" or "b" and so performs the "default" actions.

« previous php.notes (#183573) next »