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 语句


基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何替换eregi() 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01