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'; } ?>
<object type="application/x-shockwave-flash" data="file.swf" width="500" height="250"> <param name="src" value="file.swf" /> Your browser not support SWF. </object>
input:focus { background-color: #88fe88; }
var jsnstr = '{"url": "http://coursesweb.net/", "title": "Web Development Courses"}'; var obj = JSON.parse(jsnstr); alert(obj.url);
if (file_put_contents("file.txt", "content")) echo "The file was created"; else echo "The file can not be created";