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 8210263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:00:59+00:00 2026-06-07T10:00:59+00:00

Using if and elseif , it is possible to easily perform the comparison below,

  • 0

Using if and elseif, it is possible to easily perform the comparison below, but for learning purposes I am analyzing if it is possible to have the same functionality using switch.

If $x receives a positive or negative value, I get the right output, but if $x receives 0 (zero), I get the output ‘Lower’, but the right output should be ‘Equal’.

Here is the code:

$x = 0;

switch($x)
{
    case ($x < 0):
        echo 'Lower';
        break;

    case ($x == 0):
        echo 'Equal';
        break;

    case ($x > 0):
        echo 'Bigger';
        break;
    default:
        echo 'Not found';
}

Is it possible to use a switch statement with expressions as cases?

  • 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-07T10:01:01+00:00Added an answer on June 7, 2026 at 10:01 am

    You are effectively not matching numbers any more, but matching the boolean outcome.

    Any positive or negative number casts to boolean true and only 0 casts to false, so basically for a positive or negative number you are comparing true to ($x < 0) or ($x > 0) and that gives the outcome you expect.

    However, 0 casts to false and (0 == 0) is true so you will never have a match there. And as 0 < 0 is also false, your first statement in the switch is matched.

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

Sidebar

Related Questions

Possible Duplicate: Using Case/Switch and GetType to determine the object If I hope to
Possible Duplicate: Using jQuery to compare two arrays I have to send the form
Using import datetime in python, is it possible to take a formatted time/date string
I wondering if it possible to have a preprocessor OR or AND statement? I
Possible Duplicate: is “else if” faster than “switch() case” ? I've encountered a lot
Is it possible to test if a javascript cookie has expired using? I need
I am using strong typed views in my MVC and have now manage to
Possible Duplicate: Smooth text animation (Marquee) using WPF I am just wondering if there
I am using Uploadify/asp.net mvc 3 to upload images. Is it possible to display
I have to compare(>,<,==) two class object based upon different criteria, explained below. class

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.