Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7526805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:56:08+00:00 2026-05-30T03:56:08+00:00

$a = true; //b is unset; switch(true) { case ($a): echo ‘a’; case (isset($b)):

  • 0
$a = true;
//b is unset;

switch(true) {
    case ($a): echo 'a';
    case (isset($b)): echo 'b';
    default: echo 'c';
}

//outputs 'abc'. expected output is 'bc'

Why is the output not as expected?

Update: Expected output ‘bc’ is a typo and should read ‘ac’.

Update: I now see how this code produces different output than expected. EVERY code section in a switch block after the first case that returns true will be executed, unless the switch is ended by a break. After break point no other cases will be tested. This is where my mistake was as I expected testing to resume per case with the absence of break statements.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:56:10+00:00Added an answer on May 30, 2026 at 3:56 am

    Code written by Isius is correct. It’s just the nice trick to find out, which of following cases is true.

    Your code doesn’t work properly because you didn’t put break statement to each case.

    It should look like that:

    $a = true;
    //b is unset;
    
    switch(true) 
    {
        case ($a): 
                { 
                echo 'a'; 
                break;
                 }
    
        case (isset($b)): 
                { 
                echo 'b'; 
                break; 
                }
    
        default:  echo 'c';
    }
    

    Output is ‘a’. I don’t know why you’re expecting to get “bc” output.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Considering that: The isset() construct returns TRUE if a variable is set and not
True or not: We should always use proper capitalization and never put whole sentences
Is it true that the only way to handle default function arguments is through
Required is the true language version of the OS, not the user interface language
Possible Duplicate: Why is a C++ bool var true by default? Say I were
TRUE/FALSE if clauses are easily and quickly done in R. However, if the argument
while (true) { //read in the file StreamReader convert = new StreamReader(../../convert.txt); //define variables
Under Windows XP WPF true 3D content (which is usually displayed using the Viewport3D
when i use setDefaultLookAndFeelDecorated(true) method in Java why is the Frame appear FullScreen when
I want a true deep copy. In Java, this was easy, but how do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.