PHP switch statement returns invalid output

0 votes

The PHP switch statement returns invalid output :

$cond = "Night";

switch($cond){
    case "Morning":
    case "Night":
        $name = "Good";

    case "Morning" :
        echo $name." Morning";
        break;

    case "Night" :
        echo $name." Night";
        break;
}

Output: Good Morning

Expected: Good Night

Can someone help me with this?

Jun 9, 2022 in PHP by Kichu
• 19,040 points
414 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Referred from the doc:

It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. 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.

The switch construct is a double-edged sword unless you specify a break. PHP does not verify case conditions any more once it has started executing statements inside a case. Without a break, it will just continue with the statements of the next cases without verifying the case conditions.

For example: 

The following code:

switch (1) {
case 1:
    echo "one ";
case 2:
    echo "two ";
case "hello":
    echo "hello ";
}

Will output:

one two hello

PHP finds the first case condition to be true and starts executing statements without evaluating any other case conditions.

I hope this helps you.

answered Jun 11, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In PHP

0 votes
1 answer

How to include a PHP variable inside a MySQL statement?

Hii, You can try this: $query="SELECT * FROM CountryInfo ...READ MORE

answered Apr 9, 2020 in PHP by Niroj
• 82,840 points
17,533 views
0 votes
1 answer

Invalid argument supplied for foreach() on line 25. Error in my php and mysql

Hello @viven , This should be a simple fix ...READ MORE

answered Aug 4, 2020 in PHP by Niroj
• 82,840 points
4,488 views
0 votes
1 answer

Error:Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23

Hello @kartik, This error message gets triggered when anything is ...READ MORE

answered Sep 16, 2020 in PHP by Niroj
• 82,840 points
5,825 views
0 votes
1 answer

How to run process with realtime output in PHP?

Hello @kartik, This worked for me: $cmd = "ping ...READ MORE

answered Oct 20, 2020 in PHP by Niroj
• 82,840 points
4,344 views
0 votes
1 answer

How do I capture PHP output into a variable?

Hello, Try this: <?php ob_start(); ?> <xml/> <?php $xml = ob_get_clean(); ...READ MORE

answered Nov 3, 2020 in PHP by Niroj
• 82,840 points
1,349 views
0 votes
1 answer

Php exec() not returning error message in output

Hello @kartik, You need to capture the stderr too. Redirecting stderr to stdout should do ...READ MORE

answered Nov 18, 2020 in PHP by Niroj
• 82,840 points
5,264 views
+1 vote
2 answers

Scp Php files into server using gradle

Tru something like this: plugins { id ...READ MORE

answered Oct 11, 2018 in DevOps & Agile by lina
• 8,220 points
1,838 views
0 votes
1 answer

How do I create folder under an Amazon S3 bucket through PHP API?

Of Course, it is possible to create ...READ MORE

answered Apr 24, 2018 in AWS by anonymous
11,629 views
0 votes
1 answer

Failure uploading Image on AmazonS3 with PHP SDK

Try this, I took it out from ...READ MORE

answered May 4, 2018 in AWS by Cloud gunner
• 4,670 points
4,830 views
0 votes
1 answer

Trying to call AWS API via PHP

Try using AWS SDK for PHP, Link ...READ MORE

answered Jun 6, 2018 in AWS by Cloud gunner
• 4,670 points
1,901 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP