Reputation: 15848
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.
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
but it returns:
Airline Name: PLEASE PROVIDE AIRLINE NAME
Flight Number: PLEASE PROVIDE FLIGHT NUMBER
Departure Airport: PLEASE PROVIDE DEPARTURE AIRPORT
Code:
$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);
?>
Upvotes: 2
Views: 1111
Reputation: 500
There are two issues here.
First, as pointed out in the comments, your switch statement doesn't have any break
s.
Second, a switch statement can only have 1 result. So once it finds its first match, which will happen either when $one is empty or isn't empty, it will end the statement.
In this case, I would just use a series if
statements instead:
<?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);
?>
EDIT In response to your comment, take this simple example here, if you run it you'll see that the code only echos out 'first' even though all three cases are the same.
<?php
$a = 1;
switch ($a) {
case 1:
echo 'first';
break;
case 1:
echo 'second';
break;
case 1:
echo 'third';
break;
}
?>
Upvotes: 1
Reputation: 681
You can't use switch statement this way, what you are doing now is walking through all cases regardless your condition for example if you swap the first case with the second one the output will be 'United'. That is because you need to put break after each case so when find he right choice break the loop. you can use if else instead and would look simpler and smaller
first put the default value as empty
<?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);
?>
Upvotes: 0
Reputation: 23880
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);
As to why your switch
doesn't perform as you expect, per the manual:
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.
-http://php.net/manual/en/control-structures.switch.php
Upvotes: 2
Reputation: 367
Your syntax is wrong Switch statements are like this...
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;
}
And one question? Why you are using boolean to switch values ?
Upvotes: 0