PHP 中的条件 switch 语句 [英] Conditional switch statements in PHP

查看:32
本文介绍了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 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆