晕!我不确定我的 switch 语句哪里出错了!这是我希望我的代码执行的操作:
Halo! I am not sure where I am going wrong in my switch statement! Here's what I want my code to do:
如果三个变量都为空,那么我不希望任何事情发生.但是,如果没有,那么我想看看哪些是空的,哪些不是,并根据它们的状态执行不同的任务.
If all three variables are empty, then I want nothing to happen. However, if not, then I want to see which ones are empty and which ones are not and perform different tasks based on their state.
如果它们不为空,那么我想在变量之前添加一个字符串.如果它们是空的,那么我想添加一个字符串,说明..PLEASE PROVIDE INFORMATION".
If they're not empty then I want to add a string before the variable. If they're empty, then I want to add a string stating "..PLEASE PROVIDE INFORMATION".
使用当前的硬编码变量,它应该返回:
With the current hard coded variables, it should return:
Airline Name: United Flight Number: 262 Departure Airport: PLEASE PROVIDE DEPARTURE AIRPORT但它返回:
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); ?> 推荐答案您想要的行为可以通过 if else 或三元运算符来实现(这几乎只是一种快捷方式编写if/else).这是一个未经测试的粗略示例.
The behavior you want would be achieved with an if else, or the ternary operator (which is pretty much just a short hand way of writing an if/else). Here's a rough untested example.
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 没有按照您的预期执行,根据手册:
As to why your switch doesn't perform as you expect, per the manual:
只有当 case 语句的值与 switch 表达式的值匹配时,PHP 才会开始执行这些语句.PHP 会继续执行语句,直到 switch 块结束,或者第一次看到 break 语句. 如果没有在 case 语句列表的末尾编写 break 语句,PHP将继续执行以下案例的语句.
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.
-php/manual/en/control-structures.switch.php
更多推荐
Switch 语句返回错误结果
发布评论