Switch statement
Switch statements work the same as if statements. However the difference is that they can check for multiple values. Of course you do the same with multiple if..else statements, but this is not always the best approach.
A switch statement allows a program to evaluate an expression and attempt to match the expression's value to a case label. If a match is found, the program executes the associated statement. The syntax for the switch statement as follows:
examples switch case:
A simple switch case program in php that match out the input creature name with given creatures names. If it match, it will produce the string and print by echo function. If the input creature name cant match, the default case will be run and print "I have not heard of this creature" like this.
The Output will be like this:
Switch statements work the same as if statements. However the difference is that they can check for multiple values. Of course you do the same with multiple if..else statements, but this is not always the best approach.
A switch statement allows a program to evaluate an expression and attempt to match the expression's value to a case label. If a match is found, the program executes the associated statement. The syntax for the switch statement as follows:
switch (expression) { case label_1: statement_1 [break;] case label_2: statement_2 [break;] . . . case label_n: statement_n [break;] default: statement_default [break;] }
examples switch case:
A simple switch case program in php that match out the input creature name with given creatures names. If it match, it will produce the string and print by echo function. If the input creature name cant match, the default case will be run and print "I have not heard of this creature" like this.
<?php // switch example using strings $creature="u3schools"; switch ($creature){ case "google": echo "a $creature says: search engine"; break; case "u3schools": echo "a $creature says: online web tutorial"; break; case "iReport": echo "a $creature says: Action with PDF"; break; default: echo "I have not heard of this creature"; break; }; ?>
The Output will be like this:
a u3schools says: online web tutorial
0 comments:
Post a Comment