note 105988 added to control-structures.switch

From: Date: Fri, 30 Sep 2011 11:19:07 +0000
Subject: note 105988 added to control-structures.switch
Groups: php.notes 
Request: Send a blank email to php-notes+get-183539@lists.php.net to get a copy of this message
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. ---- Server IP: 69.147.83.197 Probable Submitter: 94.194.35.132 ---- Manual Page -- http://www.php.net/manual/en/control-structures.switch.php Edit -- https://master.php.net/note/edit/105988 Del: integrated -- https://master.php.net/note/delete/105988/integrated Del: useless -- https://master.php.net/note/delete/105988/useless Del: bad code -- https://master.php.net/note/delete/105988/bad+code Del: spam -- https://master.php.net/note/delete/105988/spam Del: non-english -- https://master.php.net/note/delete/105988/non-english Del: in docs -- https://master.php.net/note/delete/105988/in+docs Del: other reasons-- https://master.php.net/note/delete/105988 Reject -- https://master.php.net/note/reject/105988 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#183539) next »