PHP中的条件开关语句 [英] Conditional switch statements in PHP
问题描述
我已经很习惯vb.net的 Select Case
语法实际上是一个switch语句,您可以在其中执行Case Is > 5
之类的操作,如果匹配,它将执行这种情况.
I'm quite used to vb.net's Select Case
syntax which is essentially a switch statement, where you can do things like Case Is > 5
and if it matches, it will execute that case.
由于我不知道PHP的实际名称,我该怎么做我称之为条件开关语句"的事情?
How can I do what I'm going to call "conditional switch statements" since I don't know the actual name, in PHP?
或者,管理此问题的快捷方法是什么?
Or, what's a quick way to manage this?
switch($test)
{
case < 0.1:
// do stuff
break;
}
那是我目前尝试过的.
推荐答案
我认为您正在搜索类似的东西(这并不是您想要的,或者至少我理解的是您的需求).
I think you're searching for something like this (this is not exactly what you want or at least what I understand is your need).
switch (true)
查找评估为真实值的案例,并在其中执行代码直到第一个中断;它会遇到.
switch (true)
finds the cases which evaluate to a truthy value, and execute the code within until the first break; it encounters.
<?php
switch (true) {
case ($totaltime <= 1):
echo "That was fast!";
break;
case ($totaltime <= 5):
echo "Not fast!";
break;
case ($totaltime <= 10):
echo "That's slooooow";
break;
}
?>
这篇关于PHP中的条件开关语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!