Switch 语句 returns 不正确的结果
Switch Statement returns incorrect results
晕!我不确定我的 switch 语句哪里出错了!这是我希望我的代码执行的操作:
如果三个变量都为空,那么我不希望发生任何事情。但是,如果不是,那么我想看看哪些是空的,哪些不是,并根据它们的状态执行不同的任务。
如果它们不为空,那么我想在变量前添加一个字符串。如果它们是空的,那么我想添加一个字符串,说明“..请提供信息”。
使用当前的硬编码变量,应该return:
Airline Name: United
Flight Number: 262
Departure Airport: PLEASE PROVIDE DEPARTURE AIRPORT
但它 returns:
Airline Name: PLEASE PROVIDE AIRLINE NAME
Flight Number: PLEASE PROVIDE FLIGHT NUMBER
Departure Airport: PLEASE PROVIDE DEPARTURE AIRPORT
代码:
$airline_name = "United";
$flight_number = 262;
$departure_airport = "";
function airport($one, $two, $three) {
if ($one =="" && $two =="" && $three =="") {
} else {
switch(true) {
case !empty($one):
$one = "Airline Name: $one<br>";
case empty($one):
$one = "Airline Name: PLEASE PROVIDE AIRLINE NAME<br>";
case !empty($two):
$two = "Flight Number: $two<br>";
case empty($two):
$two = "Flight Number: PLEASE PROVIDE FLIGHT NUMBER<br>";
case !empty($three):
$three = "Departure Airport: $three<br>";
case empty($three):
$three = "Departure Airport: PLEASE PROVIDE DEPARTURE AIRPORT<br>";
}
}
echo $one, $two, $three;
}
airport($airline_name,$flight_number,$departure_airport);
?>
你的语法错误
switch语句是这样的...
switch ($some_var) {
case 'Peter': // if $some_var has value 'Peter'
# code... // this line executes if it is true, else breaks and loops through other cases until it finds right val
break;
default:
# code... // default code to be executed if case val isn't found
break;
}
还有一个问题?为什么要使用布尔值来切换值?
您想要的行为将通过 if else
或三元运算符(这几乎只是编写 if/else
的简写方式)实现。这是一个未经测试的粗略示例。
function airport($one, $two, $three) {
if ( !empty($one) || !empty($two) || !empty($three) ) {
$one = !empty($one) ? "Airline Name: $one<br>" :"Airline Name: PLEASE PROVIDE AIRLINE NAME<br>";
$two = !empty($two) ? "Flight Number: $two<br>" : "Flight Number: PLEASE PROVIDE FLIGHT NUMBER<br>";
$three = !empty($three) ? "Departure Airport: $three<br>" : "Departure Airport: PLEASE PROVIDE DEPARTURE AIRPORT<br>";
}
echo $one, $two, $three;
}
airport($airline_name,$flight_number,$departure_airport);
关于为什么您的 switch
没有按照手册执行您预期的那样:
Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case.
这里有两个问题。
首先,正如评论中指出的那样,您的 switch 语句没有任何 break
s.
其次,一个switch语句只能有1个结果。因此,一旦它找到它的第一个匹配项(当 $one 为空或不为空时都会发生),它就会结束该语句。
在这种情况下,我将只使用一系列 if
语句来代替:
<?php
$airline_name = "United";
$flight_number = 262;
$departure_airport = "";
function airport($one, $two, $three) {
if(!empty($one)){
$one = "Airline Name: $one<br>";
} else {
$one = "Airline Name: PLEASE PROVIDE AIRLINE NAME<br>";
}
if(!empty($two)){
$two = "Flight Number: $two<br>";
} else {
$two = "Flight Number: PLEASE PROVIDE AIRLINE NAME<br>";
}
if(!empty($three)){
$three = "Departure Airport: $three<br>";
} else {
$three = "Departure Airport: PLEASE PROVIDE AIRLINE NAME<br>";
}
echo $one, $two, $three;
}
airport($airline_name,$flight_number,$departure_airport);
?>
编辑
为了回应您的评论,请在此处举一个简单的示例,如果您 运行 它,您会看到代码仅回显 'first',即使所有三种情况都相同。
<?php
$a = 1;
switch ($a) {
case 1:
echo 'first';
break;
case 1:
echo 'second';
break;
case 1:
echo 'third';
break;
}
?>
您不能以这种方式使用 switch 语句,您现在所做的是遍历所有情况,而不管您的情况如何,例如,如果您将第一种情况与第二种情况交换,输出将是 'United'。那是因为你需要在每个案例之后放一个 break ,所以当找到他正确的选择时打破循环。你可以使用 if else 来代替,这样看起来会更简单更小
先把默认值设为空
<?php
$airline_name = "United";
$flight_number = 262;
$departure_airport = "";
function airport($one, $two, $three) {
if ($one =="" && $two =="" && $three =="") {
}
else
{
if ($one == '')
$one= "PLEASE PROVIDE AIRLINE NAME<br>";
if ($two == '')
$two= "PLEASE PROVIDE FLIGHT NUMBER<br>";
if ($three == '')
$three = "PLEASE PROVIDE DEPARTURE AIRPORT<br>";
$x1= "Airline Name: ".$one;
$x2= "Flight Number: ".$two;
$x3= "Departure Airport:".$three;
}
echo $x1, $x2, $x3;
}
airport($airline_name,$flight_number,$departure_airport);
?>
晕!我不确定我的 switch 语句哪里出错了!这是我希望我的代码执行的操作:
如果三个变量都为空,那么我不希望发生任何事情。但是,如果不是,那么我想看看哪些是空的,哪些不是,并根据它们的状态执行不同的任务。
如果它们不为空,那么我想在变量前添加一个字符串。如果它们是空的,那么我想添加一个字符串,说明“..请提供信息”。
使用当前的硬编码变量,应该return:
Airline Name: United
Flight Number: 262
Departure Airport: PLEASE PROVIDE DEPARTURE AIRPORT
但它 returns:
Airline Name: PLEASE PROVIDE AIRLINE NAME
Flight Number: PLEASE PROVIDE FLIGHT NUMBER
Departure Airport: PLEASE PROVIDE DEPARTURE AIRPORT
代码:
$airline_name = "United";
$flight_number = 262;
$departure_airport = "";
function airport($one, $two, $three) {
if ($one =="" && $two =="" && $three =="") {
} else {
switch(true) {
case !empty($one):
$one = "Airline Name: $one<br>";
case empty($one):
$one = "Airline Name: PLEASE PROVIDE AIRLINE NAME<br>";
case !empty($two):
$two = "Flight Number: $two<br>";
case empty($two):
$two = "Flight Number: PLEASE PROVIDE FLIGHT NUMBER<br>";
case !empty($three):
$three = "Departure Airport: $three<br>";
case empty($three):
$three = "Departure Airport: PLEASE PROVIDE DEPARTURE AIRPORT<br>";
}
}
echo $one, $two, $three;
}
airport($airline_name,$flight_number,$departure_airport);
?>
你的语法错误 switch语句是这样的...
switch ($some_var) {
case 'Peter': // if $some_var has value 'Peter'
# code... // this line executes if it is true, else breaks and loops through other cases until it finds right val
break;
default:
# code... // default code to be executed if case val isn't found
break;
}
还有一个问题?为什么要使用布尔值来切换值?
您想要的行为将通过 if else
或三元运算符(这几乎只是编写 if/else
的简写方式)实现。这是一个未经测试的粗略示例。
function airport($one, $two, $three) {
if ( !empty($one) || !empty($two) || !empty($three) ) {
$one = !empty($one) ? "Airline Name: $one<br>" :"Airline Name: PLEASE PROVIDE AIRLINE NAME<br>";
$two = !empty($two) ? "Flight Number: $two<br>" : "Flight Number: PLEASE PROVIDE FLIGHT NUMBER<br>";
$three = !empty($three) ? "Departure Airport: $three<br>" : "Departure Airport: PLEASE PROVIDE DEPARTURE AIRPORT<br>";
}
echo $one, $two, $three;
}
airport($airline_name,$flight_number,$departure_airport);
关于为什么您的 switch
没有按照手册执行您预期的那样:
Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case.
这里有两个问题。
首先,正如评论中指出的那样,您的 switch 语句没有任何 break
s.
其次,一个switch语句只能有1个结果。因此,一旦它找到它的第一个匹配项(当 $one 为空或不为空时都会发生),它就会结束该语句。
在这种情况下,我将只使用一系列 if
语句来代替:
<?php
$airline_name = "United";
$flight_number = 262;
$departure_airport = "";
function airport($one, $two, $three) {
if(!empty($one)){
$one = "Airline Name: $one<br>";
} else {
$one = "Airline Name: PLEASE PROVIDE AIRLINE NAME<br>";
}
if(!empty($two)){
$two = "Flight Number: $two<br>";
} else {
$two = "Flight Number: PLEASE PROVIDE AIRLINE NAME<br>";
}
if(!empty($three)){
$three = "Departure Airport: $three<br>";
} else {
$three = "Departure Airport: PLEASE PROVIDE AIRLINE NAME<br>";
}
echo $one, $two, $three;
}
airport($airline_name,$flight_number,$departure_airport);
?>
编辑 为了回应您的评论,请在此处举一个简单的示例,如果您 运行 它,您会看到代码仅回显 'first',即使所有三种情况都相同。
<?php
$a = 1;
switch ($a) {
case 1:
echo 'first';
break;
case 1:
echo 'second';
break;
case 1:
echo 'third';
break;
}
?>
您不能以这种方式使用 switch 语句,您现在所做的是遍历所有情况,而不管您的情况如何,例如,如果您将第一种情况与第二种情况交换,输出将是 'United'。那是因为你需要在每个案例之后放一个 break ,所以当找到他正确的选择时打破循环。你可以使用 if else 来代替,这样看起来会更简单更小
先把默认值设为空
<?php
$airline_name = "United";
$flight_number = 262;
$departure_airport = "";
function airport($one, $two, $three) {
if ($one =="" && $two =="" && $three =="") {
}
else
{
if ($one == '')
$one= "PLEASE PROVIDE AIRLINE NAME<br>";
if ($two == '')
$two= "PLEASE PROVIDE FLIGHT NUMBER<br>";
if ($three == '')
$three = "PLEASE PROVIDE DEPARTURE AIRPORT<br>";
$x1= "Airline Name: ".$one;
$x2= "Flight Number: ".$two;
$x3= "Departure Airport:".$three;
}
echo $x1, $x2, $x3;
}
airport($airline_name,$flight_number,$departure_airport);
?>