note 29095 added to control-structures.switch

From: Date: Sat, 01 Feb 2003 22:07:28 +0000
Subject: note 29095 added to control-structures.switch
Groups: php.notes 
Request: Send a blank email to php-notes+get-43279@lists.php.net to get a copy of this message
If I understand the comment from rmunn at pobox dot com correctly, the following example quoted from the top of this page is ambiguous: if ($i == 0) { print "i equals 0"; } if ($i == 1) { print "i equals 1"; } if ($i == 2) { print "i equals 2"; } switch ($i) { case 0: print "i equals 0"; break; case 1: print "i equals 1"; break; case 2: print "i equals 2"; break; } It would imply switch is equivalent to: if ($i == 0) { print "i equals 0"; } if ($i == 1) { print "i equals 1"; } if ($i == 2) { print "i equals 2"; } if ($i == 1) { print "i equals 1 again"; } which, in this case, $i == 1 would be executed twice. Switch() is actually equivalent to: if ($i == 0) { print "i equals 0"; } elseif ($i == 1) { print "i equals 1"; } elseif ($i == 2) { print "i equals 2"; } elseif ($i == 1) { print "i equals 1 again"; } else // this would be equivalent to 'default:' where $i == 1 would be executed only once. -- http://www.php.net/manual/en/control-structures.switch.php http://master.php.net/manage/user-notes.php?action=edit+29095 http://master.php.net/manage/user-notes.php?action=delete+29095 http://master.php.net/manage/user-notes.php?action=reject+29095

« previous php.notes (#43279) next »