Conditional switch statements in PHP(PHP 中的条件 switch 语句)
问题描述
我很习惯 vb.net 的 Select Case
语法本质上是一个 switch 语句,您可以在其中执行 Case Is > 之类的操作.5
,如果匹配,就会执行那个case.
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 中的条件 switch 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP 中的条件 switch 语句


- 没有作曲家的 PSR4 自动加载 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- Laravel 仓库 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01