Switch Statement returns incorrect results

Govind Rai

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);

?>
chris85

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Incorrect results with IN statement in MariaDB?

From Dev

Len function returns incorrect results

From Dev

Switch statement not displaying any results

From Dev

incorrect my sql select statement results

From Dev

Python's hasattr sometimes returns incorrect results

From Dev

Get-Unique returns incorrect results

From Dev

mysql char_length returns incorrect results

From Dev

mysql char_length returns incorrect results

From Dev

Java Sigmoid method returns Incorrect Results

From Dev

Defining a block in a switch statement results in a compiler error

From Dev

SQL statement returns no results if table in NOT IN segment is empty

From Dev

Returns “Enumeration yielded no results” in LINQ statement

From Dev

IF statement inside FOR Loop returns false results

From Dev

Returns “Enumeration yielded no results” in LINQ statement

From Dev

incorrect syntax : including results of stored procedure in select statement

From Dev

Assigning label colour in ggplot based on condition returns unexpected (and incorrect) results

From Dev

PostgREST filter returns incorrect results when value contains a space

From Dev

Elastic Search match query returns incorrect results and score

From Dev

Azure Search using Lucene Query Syntax Returns Incorrect Results

From Dev

IIf statement and If statement evaluating differently. IIf statement yields incorrect results. Why?

From Dev

Results from search not shown in iframe (using switch statement)

From Dev

multiple joins, each with IN statement returns too many results

From Dev

SELECT statement returns no results on iOS 8.2 (SQL 3.8.4)

From Dev

Cursor attribute %notfound as exit statement returns confusing results

From Dev

My if statement returns unexpected results, making random Sudoku with Javascript

From Dev

Switch on an if statement?

From Dev

Switch Statement within a switch statement

From Dev

Query builder in Codeigniter generates correct SQL statement, but executing the query leads to incorrect results

From Dev

Validate the switch statement with an if statement

Related Related

  1. 1

    Incorrect results with IN statement in MariaDB?

  2. 2

    Len function returns incorrect results

  3. 3

    Switch statement not displaying any results

  4. 4

    incorrect my sql select statement results

  5. 5

    Python's hasattr sometimes returns incorrect results

  6. 6

    Get-Unique returns incorrect results

  7. 7

    mysql char_length returns incorrect results

  8. 8

    mysql char_length returns incorrect results

  9. 9

    Java Sigmoid method returns Incorrect Results

  10. 10

    Defining a block in a switch statement results in a compiler error

  11. 11

    SQL statement returns no results if table in NOT IN segment is empty

  12. 12

    Returns “Enumeration yielded no results” in LINQ statement

  13. 13

    IF statement inside FOR Loop returns false results

  14. 14

    Returns “Enumeration yielded no results” in LINQ statement

  15. 15

    incorrect syntax : including results of stored procedure in select statement

  16. 16

    Assigning label colour in ggplot based on condition returns unexpected (and incorrect) results

  17. 17

    PostgREST filter returns incorrect results when value contains a space

  18. 18

    Elastic Search match query returns incorrect results and score

  19. 19

    Azure Search using Lucene Query Syntax Returns Incorrect Results

  20. 20

    IIf statement and If statement evaluating differently. IIf statement yields incorrect results. Why?

  21. 21

    Results from search not shown in iframe (using switch statement)

  22. 22

    multiple joins, each with IN statement returns too many results

  23. 23

    SELECT statement returns no results on iOS 8.2 (SQL 3.8.4)

  24. 24

    Cursor attribute %notfound as exit statement returns confusing results

  25. 25

    My if statement returns unexpected results, making random Sudoku with Javascript

  26. 26

    Switch on an if statement?

  27. 27

    Switch Statement within a switch statement

  28. 28

    Query builder in Codeigniter generates correct SQL statement, but executing the query leads to incorrect results

  29. 29

    Validate the switch statement with an if statement

HotTag

Archive