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

  • Home
  • SEARCH
  • 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 8390635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:03:00+00:00 2026-06-09T19:03:00+00:00

I recently got into an argument over how switch handles comparisons, and need help

  • 0

I recently got into an argument over how switch handles comparisons, and need help settling it.

If I write a switch such as:

switch ($x){
    case ($x > 5):
        echo "foo";
        break;
    case ($x < 5):
        echo "bar";
        break;
    default:
        echo "five";
        break;
}

Which if statement is it equivalent to? A or B?

// A

if ($x > 5) {
    echo "foo";
} elseif ($x < 5) {
    echo "bar";
} else {
    echo "five";
}

// B

if ($x == ($x > 5)) {
    echo "foo";
} elseif ($x == ($x < 5)) {
    echo "bar";
} else {
    echo "five";
}
  • 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-06-09T19:03:02+00:00Added an answer on June 9, 2026 at 7:03 pm

    To everyone, let me clarify:

    It is equivalent to B.

    It is not “both”, it is not sometimes its one, sometimes it is the other, it is always B. To understand why you sometimes see results that indicate that it might be A, you need to understand how type coercion works in PHP.

    If you pass in a falsey value to the “argument” of switch and you use expressions in your cases that result in a boolean value, they will only match if your expression evaluates to FALSE.


    switch is basically a huge if/elseif tree that performs loose comparisons (== instead of ===) between the value passed to switch (the left side of the expression) and the expression in the cases (the right side).

    This can be proved quite nicely with a variation on your code:

    $x = 0;
    
    switch ($x) {
      case $x > -1: // This is TRUE, but 0 == FALSE so this won't match
        echo "case 1";
      case $x == -1: // This is FALSE, and 0 == FALSE so this will match
        echo "case 2";
    }
    

    And if we convert that to the two if/elseif trees:

    A:

    $x = 0;
    
    if ($x > -1) {
      // It matches here
      echo "case 1";
    } else if ($x == -1) {
      // and not here
      echo "case 2";
    }
    

    B:

    $x = 0;
    
    if ($x == ($x > -1)) {
      // It doesn't match here
      echo "case 1";
    } else if ($x == ($x == -1)) {
      // ..but here instead
      echo "case 2";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got into a mini-argument with my boss recently regarding project failure. After three
I recently got into Java. I have a background in dynamic languages and I'm
I am programming in PHP mysql. I have recently got into OOP programming. So
I've dabbled with Haskell in the past, and recently got back into it seriously,
I just got into python very recently and now I'm practicing by (what I
Recently, I got into a problem with linking and VPATH with the side effect
I've recently been doing a lot of Objective-C programming, and just got back into
I have been using a while loop for this but recently got into debate
Recently I got into a discussion with my Team lead about using temp variables
I started playing with Racket pattern matching system recently and got into a problem

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.