I have just started learning PHP, and have got so far as the switch statement, and have grinded to a halt- I don’t understand how it works. Is there anywhere else which has decent tutorials for learning the stuff I don’t understand at w3schools?
[code=php] <?php # let's take an easy example: a php-based navigation # the page name is defined in the URL # e.g.: http://www.example.com/index.php?page=contact if(isset($_GET['page'])){ $page = $_GET['page']; }else{ # set page to contact if it is not defined in the URL $page = "contact"; } switch($page){ case "contact"; # statements if page = contact # e.g. include the contact.php page include "contact.php"; # you have to break out of the switch break; case "something"; # statements if page = something # ... # remember to break break; #... add as many cases as needed # the last one is just like the "else" of an "if-else" statement # it is called the default case case default; # statements in the default case # that will apply if the 'page' defined in the URL # is none of the cases of the switch structure # e.g. if the user enters http://example.com/index.php?page=blue # and there is no blue case, then the default case will be applied # meaning: contact.php will be included include "contact.php";
@LiLcRaZyFuZzYDec 07.2005 — #http://www.php.net/switch It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case. [/QUOTE]