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 11 1734
Ciaran 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
='s should be == I guess
if(in_array($var, array(1, 4 etc)))
is perhaps shorter, but not "more efficient", so the answer is "no".

gosha bine
extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
On 19 May 2007 05:47:38 0700, Ciaran <cr*******@hotmail.comwrote:
>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";}
depends on what you mean by more efficient but ..
if in_array ( $var, array(1, 2, 27, 28, 30, 37, 38) )

Regards, Paul Herber, Sandrila Ltd. http://www.pherber.com/
Unicode characters http://www.diacrit.sandrila.co.uk/
Email address in headers is invalid.
On May 19, 1:52 pm, gosha bine <stereof...@gmail.comwrote:
Ciaran 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
='s should be == I guess
if(in_array($var, array(1, 4 etc)))
is perhaps shorter, but not "more efficient", so the answer is "no".

gosha bine
extended php parser ~http://code.google.com/p/pihipi
blok ~http://www.tagarga.com/blok
Oops yes = should be ==
Ok I thought there might be a IN(1,4,etc) clause in PHP like other
languages.
Thanks for the reply,
Ciarán
At Sat, 19 May 2007 14:52:28 +0200, gosha bine let his monkeys type:
Ciaran 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
='s should be == I guess
if(in_array($var, array(1, 4 etc)))
is perhaps shorter, but not "more efficient", so the answer is "no".
If by 'more efficient' you mean 'executing faster', the answer is:
It depends on array length.
For short arrays in_array is slower, for long arrays it appears to be
faster. (function call overhead becomes less of a factor I suppose)
Sh.
BEGIN PGP SIGNED MESSAGE
Hash: SHA1
Ciaran 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";}
Close to the same efficiency, with a one time initialization cost:
$values = array_flip(array(1, 4, 27, 28, 30, 37, 38));
if (isset($values[$var])) echo "true";
 
Edward Z. Yang GnuPG: 0x869C48DA
HTML Purifier <htmlpurifier.org AntiXSS HTML Filter
[[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
BEGIN PGP SIGNATURE
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla  http://enigmail.mozdev.org
iD8DBQFGTxYaqTO+fYacSNoRAhQuAJ9M81xVm9mOG3puYb549G zB4pFxKACggckR
52GQSgl5Rn7Ybj0bPM8GgW0=
=S0J3
END PGP SIGNATURE
On 19 Mai, 14:47, Ciaran <cronok...@hotmail.comwrote:
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."", "122728303738")) {
echo "true";
}
purcaholic
On May 19, 6:44 pm, purcaholic <purcaho...@googlemail.comwrote:
On 19 Mai, 14:47, Ciaran <cronok...@hotmail.comwrote:
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."", "122728303738")) {
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?
Cheers,
Ciarán
BEGIN PGP SIGNED MESSAGE
Hash: SHA1
Ciaran wrote:
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?
Purcaholic's one doesn't work (as it stands: he probably mean to use
strpos) and is probably the slowest.
In terms of speed, it's probably like this (from fastest to slowest):
1. Original method
2. isset($lookup[$var])
3. in_array
4. strstr
If you have a particularly large set of numbers to compare against,
method 2 may end up being faster due to the implementation of
associative arrays needing only a binary search. I would use method 2
simply because it's more maintainable (you can generate the $lookup
array from anywhere you want).
But I should warn you: this is really premature optimization and will
make no difference at all most of the time.
 
Edward Z. Yang GnuPG: 0x869C48DA
HTML Purifier <htmlpurifier.org AntiXSS HTML Filter
[[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
BEGIN PGP SIGNATURE
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla  http://enigmail.mozdev.org
iD8DBQFGT0CzqTO+fYacSNoRAt6ZAJ9stC/G1vPm0orjhfegDK6vRvsQhwCeNz9V
iwhkovqnMTPpNIzUXH9Ff9I=
=Qajq
END PGP SIGNATURE
On 19 Mai, 20:23, "Edward Z. Yang" <edwardzy...@thewritingpot.com>
wrote:
BEGIN PGP SIGNED MESSAGE
Hash: SHA1
Ciaran wrote:
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?
Purcaholic's one doesn't work (as it stands: he probably mean to use
strpos) and is probably the slowest.
In terms of speed, it's probably like this (from fastest to slowest):
1. Original method
2. isset($lookup[$var])
3. in_array
4. strstr
If you have a particularly large set of numbers to compare against,
method 2 may end up being faster due to the implementation of
associative arrays needing only a binary search. I would use method 2
simply because it's more maintainable (you can generate the $lookup
array from anywhere you want).
But I should warn you: this is really premature optimization and will
make no difference at all most of the time.
 
Edward Z. Yang GnuPG: 0x869C48DA
HTML Purifier <htmlpurifier.org AntiXSS HTML Filter
[[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
BEGIN PGP SIGNATURE
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla http://enigmail.mozdev.org
iD8DBQFGT0CzqTO+fYacSNoRAt6ZAJ9stC/G1vPm0orjhfegDK6vRvsQhwCeNz9V
iwhkovqnMTPpNIzUXH9Ff9I=
=Qajq
END PGP SIGNATURE
Sorry guys,
my example was wrong, but following should work:
if (strstr("122728303738", "".$var."") !== false) {
echo "true";
}
Yes, this is not the best way, if there are much more numbers to
check, but a one line code for a simple check for a few numbers.
purcaolic
Ciaran kirjoitti:
On May 19, 6:44 pm, purcaholic <purcaho...@googlemail.comwrote:
>On 19 Mai, 14:47, Ciaran <cronok...@hotmail.comwrote:
>>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."", "122728303738")) { 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 switchcase:
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 pipeseparated 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.
 Ra*********@gmail.com
"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa."  z00ze
On 19.05.2007 16:18 Schraalhans Keukenmeester wrote:
At Sat, 19 May 2007 14:52:28 +0200, gosha bine let his monkeys type:
>Ciaran 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
='s should be == I guess
if(in_array($var, array(1, 4 etc)))
is perhaps shorter, but not "more efficient", so the answer is "no".
If by 'more efficient' you mean 'executing faster', the answer is:
It depends on array length.
For short arrays in_array is slower, for long arrays it appears to be
faster. (function call overhead becomes less of a factor I suppose)
Sh.
'more efficient' means 'runs faster' and 'uses less memory'. I think
'or' code will win in both categories.

gosha bine
extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok This discussion thread is closed Replies have been disabled for this discussion. Similar topics
1 post
views
Thread by Rookie 
last post: by

7 posts
views
Thread by Guy Hocking 
last post: by

7 posts
views
Thread by Shaldaman 
last post: by

2 posts
views
Thread by Water Cooler v2 
last post: by

12 posts
views
Thread by Brad Baker 
last post: by

3 posts
views
Thread by Simon Hart 
last post: by

43 posts
views
Thread by dev_cool 
last post: by

8 posts
views
Thread by JackC 
last post: by
           