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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:37:29+00:00 2026-05-12T11:37:29+00:00

How would you do this PHP switch statement? Also note that these are much

  • 0

How would you do this PHP switch statement?

Also note that these are much smaller versions, the one I need to create will have a lot more values added to it.

Version 1:

switch ($p) { 
    case 'home': 
    case '': 
        $current_home = 'current';
    break; 
    
    case 'users.online': 
    case 'users.location': 
    case 'users.featured': 
    case 'users.new': 
    case 'users.browse': 
    case 'users.search': 
    case 'users.staff': 
        $current_users = 'current';
    break;
     
    case 'forum': 
        $current_forum = 'current';
    break; 
} 

Version 2:

switch ($p) { 
    case 'home': 
        $current_home = 'current';
    break; 
    
    case 'users.online' || 'users.location' || 'users.featured' || 'users.browse' || 'users.search' || 'users.staff': 
        $current_users = 'current';
    break;
    
    case 'forum': 
        $current_forum = 'current';
    break; 
} 

Test Results:

I ran some speed test on 10,000 iterations,

Time 1: 0.0199389457703 // If statements
Time 2: 0.0389049446106 //switch statements
Time 3: 0.106977939606 // Arrays

  • 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-12T11:37:29+00:00Added an answer on May 12, 2026 at 11:37 am

    For any situation where you have an unknown string and you need to figure out which of a bunch of other strings it matches up to, the only solution which doesn’t get slower as you add more items is to use an array, but have all the possible strings as keys. So your switch can be replaced with the following:

    // used for $current_home = 'current';
    $group1 = array(
            'home'  => True,
            );
    
    // used for $current_users = 'current';
    $group2 = array(
            'users.online'      => True,
            'users.location'    => True,
            'users.featured'    => True,
            'users.new'         => True,
            'users.browse'      => True,
            'users.search'      => True,
            'users.staff'       => True,
            );
    
    // used for $current_forum = 'current';
    $group3 = array(
            'forum'     => True,
            );
    
    if(isset($group1[$p]))
        $current_home = 'current';
    else if(isset($group2[$p]))
        $current_users = 'current';
    else if(isset($group3[$p]))
        $current_forum = 'current';
    else
        user_error("\$p is invalid", E_USER_ERROR);
    

    This doesn’t look as clean as a switch(), but it is the only fast solution which doesn’t include writing a small library of functions and classes to keep it tidy. It is still very easy to add items to the arrays.

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

Sidebar

Ask A Question

Stats

  • Questions 216k
  • Answers 216k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Adapting astander's answer to get both the emp_no and owner_id:… May 12, 2026 at 11:11 pm
  • Editorial Team
    Editorial Team added an answer Nevermind, I found the answer, see http://www.mail-archive.com/android-developers@googlegroups.com/msg66448.html Cursor cursor =… May 12, 2026 at 11:11 pm
  • Editorial Team
    Editorial Team added an answer $("input[type=checkbox]:checked").each ( function() { alert ( $(this).val() ); }); or… May 12, 2026 at 11:11 pm

Related Questions

Let's say I have base class FooParent , and it has a numerous amount
What do I want to achieve? We are currently working on a PHP project
I've written a bookmarklet to look a word up in a Chinese dictionary: javascript:Qr=document.getSelection();if(!Qr){void(Qr=prompt('%E8%AF%8D%E8%AF%AD',''))};if(Qr)(function(){window.open('http://nciku.com/search/all/'+Qr);})();
I'm sending some data over AJAX to a PHP file. It's about filter options.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.