Switch is another type of conditional, best used in place of a long if-elseif-else conditional.
Switch statement selects one of many blocks of code to be executed, by comparing the value of a single variable with a succession of values.
switch ($variable) { case 'value1': // Do this break; case 'value2': // Run other code break; default: // if nothing else is true, do this }- The value of the $variable within the brackets is the item being tested.
The break statements are optional, but if you don't use them, PHP will continue to evaluate the next case and may execute some code further down the case tree.
The default statement is used if no match is found.
<?php $num = 2; switch($num) { case 1: echo 'Small number'; break; case 2: echo 'Medium number'; break; case 3: echo 'Bigger number'; break; default: echo 'No number between 1 and 3'; } ?>This code will output: "Medium number".
<?php switch($today) { case 'Monday': echo 'Today is Monday, I go to school.'; break; case 'Tuesday': echo 'Tuesday, lot of work to do.'; break; case 'Sunday': echo 'Sunday, good day for rest.'; break; default: echo 'A normal day'; } ?>
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net