|
Posted by Rami Elomaa on 05/19/07 19:28
Ciaran kirjoitti:
> On May 19, 6:44 pm, purcaholic <purcaho...@googlemail.com> wrote:
>> On 19 Mai, 14:47, Ciaran <cronok...@hotmail.com> wrote:
>>
>>> Is there a more efficient way to write this:
>>> if($var=1 || $var=4 || $var=27 || $var=28 || $var=30 || $var=37 ||
>>> $var=38){echo "true";}
>>> Cheers,
>>> Ciarán
>> Other way to simplify it is to compose a string using a delemiter
>> character and to search inside composed string, like:
>>
>> if (strstr("|".$var."|", "|1|2|27|28|30|37|38|")) {
>> echo "true";
>>
>> }
>>
>> purcaholic
>
>
> All great ideas guys! Thanks for the replies on this. I especially
> like purcaholic's one! Anyone know which of these methods would be the
> fastest? Are any of them actually any faster than the basic one I
> posted at the start?
I could think of one more way, using a switch-case:
switch($var){
case 1:
case 4:
case 27:
...
case 38:
echo "true";
break;
default:
echo "false";
}
In terms of code lines this is by far the longest, but I would assume
that it is fast, but not the fastest. Making the pipe-separated string
list and comparing against that would be easy to write, but on the other
hand slowest to execute. The solutions where an array is used are both
fast performing and easy to write, so I suggest you pick one of the
solutions suggested by Paul and Edward.
--
Rami.Elomaa@gmail.com
"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Navigation:
[Reply to this message]
|