Switch 语句返回错误结果

编程入门 行业动态 更新时间:2024-10-11 21:22:56
本文介绍了Switch 语句返回错误结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

晕!我不确定我的 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 语句返回错误结果

本文发布于:2023-10-19 17:53:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   错误   Switch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!