PHP案例声明快速参考

41 阅读3分钟

*{box-sizing: border-box;} body {margin: 0;}*{box-sizing:border-box;}body{margin-top:0px; margin-right:0px;margin-bottom:0px;margin-left:0px;}。

你是否曾想将一个变量或表达式的值与多个可能的结果进行比较,并有条件地执行一些代码?有很多选择可以让你做到这一点。你可以使用多个if-else条件,也可以使用switch-case语句。

在本教程中,我将告诉你如何在PHP中使用switch-case来比较一个有许多不同值的变量。

使用switch-caseif-else :直接比较

让我们从一个简单的例子开始,展示使用if-elseswitch-case 来控制程序流程的直接比较。

<?php

$animals = ['Elephant', 'Lion', 'Deer', 'Goat', 'Crocodile', 'Zebra', 'Horse'];

shuffle($animals);

$animal = $animals[0];

// Outputs: Lions are in the south-west section of the zoo.
if($animal == 'Elephant') {
    echo 'Elephants are in the south section of the zoo.';

} elseif($animal == 'Lion') {
    echo 'Lions are in the south-west section of the zoo.';

} elseif($animal == 'Deer') {
    echo 'Deer are in the north section of the zoo.';

} elseif($animal == 'Goat') {
    echo 'Goats are in the east section of the zoo.';
} else {
    echo 'Ask a tour guide.';
}

// Outputs: Lions are in the south-west section of the zoo.
switch($animal) {
    case 'Elephant':
        echo 'Elephants are in the south section of the zoo.';
        break;
    case 'Lion':
        echo 'Lions are in the south-west section of the zoo.';
        break;
    case 'Deer':
        echo 'Deer are in the north section of the zoo.';
        break;
    case 'Goat':
        echo 'Goats are in the east section of the zoo.';
        break;
    default:
        echo 'Ask a tour guide.';
}
?>

试着多次运行上述代码以获得不同的输出。你会注意到一些事情。

每个case 语句类似于一个ifelseif 块。你从if-else 部分得到的输出与从switch-case 部分得到的输出是一样的。永远记得使用break 语句来停止每个case 语句的代码执行。

假设你忘记在每个case 块中包含break ,并且$animal 的值是'Lion'。在这种情况下,代码将输出狮子、鹿和山羊的位置,以及关于导游的默认声明。

default 语句就像一个总括性的东西,处理所有其他的$animal 的值。它类似于我们在if-else 部分使用的else 块。记住,你不能在你的代码中使用多个default 语句。

同时检查多个条件

如果在动物园的同一区域有多个动物,你该怎么办?使用if-else 块,你可以把它们都放在一个条件语句中,使用|| ,看其中是否有评价为true 。 下面是一个例子。

$animal = 'Zebra';

// Outputs: Zebras are in the west section of the zoo.
if($animal == 'Elephant' || $animal == 'Lion' || $animal == 'Goat') {
    echo $animal.'s are in the south section of the zoo.';
} elseif($animal == 'Zebra') {
    echo 'Zebras are in the west section of the zoo.';
} elseif($animal == 'Deer') {
    echo 'Deer are in the east section of the zoo.';
} else {
    echo 'Ask a tour guide.';
}

上面的代码会正确地告诉我们,斑马在动物园的西区。它跳过了第一个if 块内的代码,因为只有当$animal 是大象、狮子或山羊时才会执行。

让我们以switch-case 语句的形式重写上述代码。你可能很想把它写成如下所示。

// Outputs: Zebras are in the east section of the zoo.
switch($animal) {
    case 'Elephant' || 'Lion' || 'Goat':
        echo $animal.'s are in the east section of the zoo.';
        break;
    case 'Zebra':
        echo 'Zebras are in the west section of the zoo.';
        break;
    case 'Deer':
        echo 'Deer are in the north section of the zoo.';
        break;
    default:
        echo 'Ask a tour guide.';
}

仔细观察,你会发现现在的语句说斑马在动物园的东边。这里发生了什么?

我们传递给switch 的值或表达式与我们在每个case 语句中传递的值或表达式进行了松散的比较。现在,'Elephant' || 'Lion || 'Goat' 最终评估为true 。当与$animal 内的值进行松散比较时,这也会返回true ,因为一个非空的字符串是一个真值。然后执行第一个case语句的代码,我们得到了同一个zoo的错误位置。

处理应该执行同一段代码的多个case 语句的正确方法是把case值写在不同的行上,如下图所示。

// Outputs: Zebras are in the west section of the zoo.
switch($animal) {
    case 'Elephant':
    case 'Lion':
    case 'Goat':
        echo $animal.'s are in the east section of the zoo.';
        break;
    case 'Zebra':
        echo 'Zebras are in the west section of the zoo.';
        break;
    case 'Deer':
        echo 'Deer are in the north section of the zoo.';
        break;
    default:
        echo 'Ask a tour guide.';
}

现在我们得到了斑马的正确位置,没有游客在寻找它们时迷路。

重复评估和性能

还有一点你应该记住的是,当使用switch 语句时,条件只被评估一次。另一方面,当使用if-else 语句时,它会被多次评估,即每个if 块评估一次。下面是一个例子。

function most_populated($country) {
    return 'New York';
}

// Outputs: This is not surprising at all.
switch(most_populated('United States')) {
    case 'New York':
        echo 'This is not surprising at all.';
        break;
    case 'Oakland':
        echo 'This does not seem correct.';
        break;
}

我们调用most_populated() ,该函数返回一个城市的名称。在这种情况下,它只是简单地返回纽约,但它可以很容易地变得更复杂,从一个网络服务中获取所传递国家的城市数据。在这种情况下,编写多个if-else条件会导致多次调用most_populated() 函数。

显然,我们可以通过将调用结果存储在一个变量中来避免所有这些调用,但使用switch可以让你根本不使用任何变量。

最后的思考

在本教程中,我向大家展示了如何在PHP中使用switch-case 语句来代替if-else 块来有条件地执行代码。我们还看到如何编写涵盖多个条件的case 语句。最后,我们了解到在某些情况下,使用switch-case 可以比if-else 块更快。