$v = 1;
switch (true) {
case ($v == 1 || $v == 2):
echo 'the value is either 1 or 2';
break;
}
如果不是,你更喜欢的解决方案是
switch($v) {
case 1:
case 2:
echo "the value is either 1 or 2";
break;
}
问题是这两种方法在处理大型案件时效率都不高。想象1到100,这将完美地工作
$r1 = range(1, 100);
$r2 = range(100, 200);
$v = 76;
switch (true) {
case in_array($v, $r1) :
echo 'the value is in range 1 to 100';
break;
case in_array($v, $r2) :
echo 'the value is in range 100 to 200';
break;
}
case 4:
echo "This is not the number you're looking for.\n";
$foo = 92;
break;
case 5:
echo "A copy of Ringworld is on its way to you!\n";
$foo = 34;
break;
case 3:
case 4:
echo "This is not the number you're looking for.\n";
$foo = 92;
break;
case 5:
echo "A copy of Ringworld is on its way to you!\n";
$foo = 34;
break;
switch(true)
{
case (strlen($foo) > 30):
$error = "The value provided is too long.";
$valid = false;
break;
case (!preg_match('/^[A-Z0-9]+$/i', $foo)):
$error = "The value must be alphanumeric.";
$valid = false;
break;
default:
$valid = true;
break;
}
$today = date("D");
switch($today){
case "Mon":
case "Tue":
echo "Today is Tuesday or Monday. Buy some food.";
break;
case "Wed":
echo "Today is Wednesday. Visit a doctor.";
break;
case "Thu":
echo "Today is Thursday. Repair your car.";
break;
default:
echo "No information available for that day.";
break;
}