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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:34:57+00:00 2026-05-17T20:34:57+00:00

I need to solve a problem like below. I’m not sure, can you suggest

  • 0

I need to solve a problem like below. I’m not sure, can you suggest better method?

switch(true){
    case $price>100:
        tooHigh(); 
    break;
    case (($price<=100) && ($price>70)):
        negotiate(); 
    break;
    case (($price<=70) && ($price>20)):
        accept(); 
    break;
    case ($price<=20):
        thankAndEscape(); 
    break;
}
  • 1 1 Answer
  • 2 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-17T20:34:57+00:00Added an answer on May 17, 2026 at 8:34 pm
    
    if ($price <= 20) {
            thankAndEscape(); 
    } elseif ($price <= 70) {
            accept();
    } elseif ($price <= 100) {
            negotiate(); 
    } else {
            tooHigh(); 
    }
    
    1. switch(true) is necessary in a small number of cases; the fact you make the check with expressions inside the case keyword means the syntax does not fit at all, that’s a good reason to use the if…elseif construct.
    2. switch…case syntax has higher performances in PHP than if…else in case there’s not a default case, so given the solution I suggest (which has a default case) I would not use the switch…case syntax.
    3. By making progressive checks, starting from the lowest price range and increasing it step by step, you don’t have to check for the whole range, since the first point of the interval is surely greater than the last of the previous check. The solution I suggest is simpler and solid, giving moreover better performances.

    Try it and post the results/your impressions (information about performances are appreciated 🙂

    Cheers!

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

Sidebar

Related Questions

I don't like JSF but I need to solve this problem with it, I
I need to solve a job affectation problem and I would like to find
Update : Unfortunately the help offered below did not solve my problem of sharing
I need to solve a problem within job booking software and am hoping to
I need to solve a locking problem for this scenario: A multi CPU system.
I need to solve such a problem. There is a base class and two
To solve some problem I need to compute a variant of the pascal's triangle
Well i need some help here i don't know how to solve this problem.
I need a regular expression to solve the following problem (links to similar problems
This is a problem I've been struggling to solve for a while. I need

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.