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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:37:48+00:00 2026-06-14T13:37:48+00:00

Maybe this is a stupid question but what is faster? <?php function getCss1 ($id

  • 0

Maybe this is a stupid question but what is faster?

<?php

function getCss1 ($id = 0) {
    if ($id == 1) {
        return 'red';
    } else if ($id == 2) {
        return 'yellow';
    } else if ($id == 3) {
        return 'green';
    } else if ($id == 4) {
        return 'blue';
    } else if ($id == 5) {
        return 'orange';
    } else {
        return 'grey';
    }
}

function getCss2 ($id = 0) {
    $css[] = 'grey';
    $css[] = 'red';
    $css[] = 'yellow';
    $css[] = 'green';
    $css[] = 'blue';
    $css[] = 'orange';
    return $css[$id];
}

echo getCss1(3);
echo getCss2(3);
?>

I suspect is faster the if statement but I prefere to ask!

  • 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-14T13:37:49+00:00Added an answer on June 14, 2026 at 1:37 pm

    getCss1 (if statements) is roughly twice as fast getCss2 (array access) from my benchmarks.

    Results from microtime():

    getCss1 called 10,000 times in 0.016569852828979 seconds
    getCss2 called 10,000 times in 0.037255048751831 seconds
    

    Per the comments, I agree about maintainability of using a translation array. Accessing $css directly is significantly faster given the removal of function overhead and array redeclaration.

    getCss1 called 10,000 times in 0.016607999801636 seconds
    $css accessed 10,000 times in 0.0026898384094238 seconds
    

    Note: Running PHP 5.3.15 on Mac OS X 10.8. I also varied call order $id to test execution paths.

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

Sidebar

Related Questions

maybe this is a stupid question but : i run perl 5.8.8 and i
Well, maybe it is a stupid question, but I cannot resolve this problem. In
Maybe this is a very stupid question but... Why should I use tinytext and
Maybe this is a stupid question, but at the end of my pages i
I know maybe this is a stupid question, But i suck at sql But
Maybe this is a stupid question. In php application I used to insert properly
Maybe this is a stupid question but it's bugging me. I have a bi-directional
I know this maybe a stupid question but maybe I'm just confused with the
This is maybe a stupid question, but I figured I'd ask before I did
maybe this is a stupid question, but i have seen many Tuts that build

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.