473,395 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Simplifying a basic if statement conditional

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
11 1837
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
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
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
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
-----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
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
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
-----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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rookie | last post by:
I have done a lot of programming some time ago using Fortran and various varieties of the Basic programming languages, Hewlett Packard Technical Basic, QBasic, Quick Basic. With each of these...
7
by: Guy Hocking | last post by:
Hi there, I have a problem in my ASP/SQL Server application i am developing, i hope you guys can help. I have a ASP form with list boxes populated by SQL tables. When a user selects a value...
7
by: Shaldaman | last post by:
I have an Access form called "Login". "Command10" is a button on the form and "Text5" is a text field on it. When a user enters a value in the Text5 text field and clicks the button Command10, I...
2
by: Water Cooler v2 | last post by:
I've not touched SQL server programming since 1999. I have very little memory of it and need some clarifications on some basic questions that I could even use a book for. Until I get myself a good...
12
by: Brad Baker | last post by:
I am trying to write a simple ASP.net/C# page which allows users to select some values and produce a report based on a SQL query. I have a self posting dropdown form which allows users to select...
3
by: Simon Hart | last post by:
Hi, I am trying to implement some functionality as seen in MS CRM 3.0 whereby a basic Xml is deserialized into an object which contains properties. What I want to do from here is; cast the basic...
43
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. ...
8
by: JackC | last post by:
Hi, I am trying to get posix threads working from within an object, heres some code: int NConnection::TheadControl() { int thread_id; pthread_t new_connection;...
7
by: tiptap | last post by:
Hey Guys, I have a huge statement loads of if statements in... and its getting bigger. On closer inspection there is only 3 difference in the select statement. so I thought I could cut the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.