In PHP is faster to get a value from an if statement or from an array?
- by Vittorio Vittori
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!