In PHP is faster to get a value from an if statement or from an array?

Posted by Vittorio Vittori on Stack Overflow See other posts from Stack Overflow or by Vittorio Vittori
Published on 2012-11-15T22:40:17Z Indexed on 2012/11/15 23:00 UTC
Read the original article Hit count: 250

Filed under:
|
|

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!

© Stack Overflow or respective owner

Related posts about php

Related posts about optimization