By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

Simplifying a basic if statement conditional

P: n/a
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

May 19 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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
May 19 '07 #2

P: n/a
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.
May 19 '07 #3

P: n/a
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

May 19 '07 #4

P: n/a
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.

May 19 '07 #5

P: n/a
-----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 Anti-XSS 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-----
May 19 '07 #6

P: n/a
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."|", "|1|2|27|28|30|37|38|")) {
echo "true";
}
purcaholic

May 19 '07 #7

P: n/a
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."|", "|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?

Cheers,
CiarŠn

May 19 '07 #8

P: n/a
-----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 Anti-XSS 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-----
May 19 '07 #9

P: n/a
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 Anti-XSS 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("|1|2|27|28|30|37|38|", "|".$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

May 19 '07 #10

P: n/a
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."|", "|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.

--
Ra*********@gmail.com

"Wikipedia on všhšn niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedš mikš pitšš paikkansa." -- z00ze
May 19 '07 #11

P: n/a
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
May 21 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.