[英]Conditional switch statements in PHP
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.我非常习惯 vb.net 的
Select Case
语法,它本质上是一个 switch 语句,您可以在其中执行Case Is > 5
之类的操作,如果匹配,它将执行该 case。
How can I do what I'm going to call "conditional switch statements" since I don't know the actual name, in PHP?由于我不知道 PHP 中的实际名称,我该如何做我将称之为“条件切换语句”的事情?
Or, what's a quick way to manage this?或者,什么是管理它的快速方法?
switch($test)
{
case < 0.1:
// do stuff
break;
}
That's what I've tried currently.这就是我目前尝试过的。
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)
finds the cases which evaluate to a truthy value, and execute the code within until the first break; switch (true)
查找评估为真值的情况,并在第一次中断之前执行其中的代码; 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 supports switch statements . PHP 支持switch 语句。 Is that what you wanted?
那是你想要的吗?
I tried to add this as a comment to the answer by BoltCock, but SO is telling me that his answer is locked so I'll make this a separate (and essentially redundant) answer:我试图将其添加为 BoltCock 答案的评论,但 SO 告诉我他的答案已被锁定,因此我将其作为一个单独的(并且基本上是多余的)答案:
The "switch(true)" answer from BoltCock is much like the following example , which although logically equivalent to if + else if + else is arguably more beautiful because the conditional expressions are vertically aligned, and is standard/accepted practice in PHP. BoltCock 的“switch(true)”答案很像下面的例子,虽然逻辑上等价于 if + else if + else 可以说更漂亮,因为条件表达式是垂直对齐的,并且是 PHP 中的标准/接受的做法。
But the if + else if + else syntax is essentially universal across scripting languages and therefore immediately readable (and maintainable) by anyone, which gives it my nod as well.但是 if + else if + else 语法本质上是跨脚本语言通用的,因此任何人都可以立即阅读(和维护),这也让我点头。
You can accomplish this in PHP 8.0+ with match statements .您可以在 PHP 8.0+ 中使用match statements完成此操作。
If you have a single expression in your switch statement, it is equivalent to the following match statement.如果您的 switch 语句中只有一个表达式,则它等效于以下 match 语句。 Note that unlike switch statements, we can choose to return a variable (in this code I am storing the result in
$result
).请注意,与 switch 语句不同,我们可以选择返回一个变量(在此代码中,我将结果存储在
$result
)。
$test = 0.05;
$result = match (true) {
$test < 0.1 => "I'm less than 0.1",
$test < 0.01 => "I'm less than 0.01",
default => "default",
};
var_dump($result);
Match only allows you to have a single expression on the right-hand side (everything after =>
). Match 只允许您在右侧有一个表达式(
=>
之后的所有内容)。 The equivalent to switch statement with multi-line cases would be to use separate functions.与多行情况下的 switch 语句等效的是使用单独的函数。
function tinyCase(){}
function veryTinyCase(){}
function defaultCase(){}
$test = 0.01;
match (true) {
$test < 0.1 => tinyCase(),
$test < 0.1 => veryTinyCase(),
default => defaultCase(),
};
And for reference, if you just want to check for equality you can do:作为参考,如果您只想检查相等性,您可以执行以下操作:
$result = match ($test) { // This example shows doing something other than match(true)
0.1 => "I'm equal to 0.1",
0.01 => "I'm equal to 0.01",
default => "default",
};
Match statements do have some key differences from switch statements that you need to watch out for: Match 语句确实与您需要注意的 switch 语句有一些主要区别:
===
instead of a loose one ==
.===
而不是松散的==
。break
.break
,也不必担心会遇到下一个案例。Switch statement with conditions带条件的 Switch 语句
switch(true)
{
case ($car == "Audi" || $car == "Mercedes"):
echo "German cars are amazing";
break;
case ($car == "Jaguar"):
echo "Jaguar is the best";
break;
default:
echo "$car is Ok";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.