PHP/Statement/switch statement — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 07:07, 26 мая 2010
Содержание
- 1 A switch Statement
- 2 A switch Statement for string
- 3 In a switch/case block, you specify what you are checking against, then give a list of possible values you want to handle.
- 4 Omitting the break statement can be useful in some cases
- 5 Switch and constants
- 6 switch command for string value
- 7 Switch Statement
- 8 Use the "default" case
- 9 Using endswitch to end the switch definition
- 10 Using switch to test for multiple values
- 11 Using the DEFAULT: statement to generate an error
- 12 Using the switch Statement
- 13 What happens when there are no break keywords
A switch Statement
<html>
<head><title>A switch Statement</title></head>
<body>
<div>
<?php
$satisfied = "no";
switch ( $satisfied ) {
case "very":
print "very";
break;
case "no":
print "no";
break;
default:
print "default";
}
?>
</div>
</body>
</html>
A switch Statement for string
<html>
<head>
<title>A switch Statement</title>
</head>
<body>
<?php
$mood = "sad";
switch ( $mood ) {
case "happy":
print "I"m in a good mood";
break;
case "sad":
print "Don"t be down!";
break;
default:
print "Neither happy nor sad but $mood";
}
?>
</body>
</html>
In a switch/case block, you specify what you are checking against, then give a list of possible values you want to handle.
<?php
$Name = "Bob";
switch($Name) {
case "Jim":
print "Your name is Jim\n";
break;
case "Sally":
print "Your name is Sally\n";
break;
default:
print "I don"t know your name!\n";
}
?>
Omitting the break statement can be useful in some cases
<?
$aColor = "Red";
switch( $aColor )
{
case "red":
case "Red":
// The following prints if $aColor is
// either "red" or "Red"
print( "#FF0000<br>" );
break;
case "green":
case "Green":
print( "#00FF00<br>" );
break;
case "blue":
case "Blue":
print( "#0000FF<br>" );
break;
default:
print( "other<br>" );
break;
}
?>
Switch and constants
<?php
define("ALIGN_LEFT", 1);
define("ALIGN_CENTER", 2);
define("ALIGN_RIGHT", 3);
switch($value) {
case ALIGN_LEFT :
break;
case ALIGN_CENTER :
break;
case ALIGN_RIGHT :
break;
}
?>
switch command for string value
<?php
$choice = "Rock";
switch ($choice){
case "Pop":
print "Pop not Rock";
break;
case "Rap":
print "Rap and not Rock";
break;
case "Jazz":
print "Jazz not Rock";
break;
case "Rock":
print "Rock music";
break;
}
?>
Switch Statement
<html>
<head>
<title>Switch Statement</title>
</head>
<body>
<?php
$num = 2;
switch($num)
{
case 1 : echo("This is case 1 code"); break;
case 2 : echo("This is case 2 code"); break;
case 3 : echo("This is case 3 code"); break;
default : echo("This is default code");
}
?>
</body>
</html>
Use the "default" case
<?
$nIndex = 17;
switch ( $nIndex )
{
case 0:
print( "zero<br>" );
break;
case 1:
print( "one<br>" );
break;
case 2:
print( "two<br>" );
break;
default:
print( "neither zero, one nor two<br>" );
break;
}
?>
Using endswitch to end the switch definition
<?
$action= "ADD";
switch ($action):
case "ADD":
echo "Perform actions for adding.";
break;
case "MODIFY":
echo "Perform actions for modifying.";
break;
case "DELETE":
echo "Perform actions for deleting.";
break;
default:
echo "Error: Action must be either ADD, MODIFY, or DELETE.";
endswitch;
?>
Using switch to test for multiple values
<?
$action = "ADD";
switch ($action) {
case "ADD":
echo "Perform actions for adding.";
break;
case "MODIFY":
echo "Perform actions for modifying.";
break;
case "DELETE":
echo "Perform actions for deleting.";
break;
}
?>
Using the DEFAULT: statement to generate an error
<?
$action= "ADD";
switch ($action) {
case "ADD":
echo "Perform actions for adding.";
break;
case "MODIFY":
echo "Perform actions for modifying.";
break;
case "DELETE":
echo "Perform actions for deleting.";
break;
default:
echo "Error: Action must be either ADD, MODIFY, or DELETE.";
}
?>
Using the switch Statement
<?php
$i = 0;
switch($i) {
case 1:
echo "First case<BR>";
case 2:
echo "Second case<BR>";
break;
default:
echo "Default case";
}
?>
What happens when there are no break keywords
<?
$action = "ORDER";
switch ($action) {
case "ORDER":
echo "order assembly.<br />";
case "PACKAGE":
echo "packing.<br />";
case "SHIP":
echo "shipping.<br />";
}
?>