473,811 Members | 2,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Alternative to this code?

I know there is a better way to do something like this in PHP:

if (($color == "red") || ($color == "blue") || ($color == "white") ||
($color == "yellow")) { .. }

Is there a way I can condense that so I don't have to write $color
everytime?
Aug 15 '05 #1
7 1115

$colors = array(
'red',
'blue',
'white',
'yellow
);
if (in_array($colo r, $colors))
{
//...
}
"Dave Thomas" <ge****@hotmail .com> wrote in message
news:1d******** ************@ad elphia.com...
|I know there is a better way to do something like this in PHP:
|
| if (($color == "red") || ($color == "blue") || ($color == "white") ||
| ($color == "yellow")) { .. }
|
| Is there a way I can condense that so I don't have to write $color
| everytime?
Aug 15 '05 #2
Dave Thomas wrote:
I know there is a better way to do something like this in PHP:

if (($color == "red") || ($color == "blue") || ($color == "white") ||
($color == "yellow")) { .. }

Is there a way I can condense that so I don't have to write $color
everytime?

switch ($color) {
case 'red':
case 'blue':
case 'yellow':
case 'white':
print "yes, its one on those\n";
break;
case 'black':
default:
print "No, its something else\n";
break;
}

HTH

C.
Aug 15 '05 #3
Yet another method:

$pattern = '/^(red|blue|whit e|yellow)$/';

if (preg_match($pa ttern, $color)) {
...
}

Aug 15 '05 #4
On 15 Aug 2005 13:40:06 -0700, kh***@dis.umsme d.edu wrote:
Yet another method:

$pattern = '/^(red|blue|whit e|yellow)$/';

if (preg_match($pa ttern, $color)) {
...
}


Just _what_ are you nattering on about?
Oh, I see the problem:
X-Trace: posting.google. com

Straighten up and fly right:
http://groups.google.com/googlegroup...html#summarize
http://oakroadsystems.com/genl/unice.htm#quote
Aug 15 '05 #5
Hoy en la tarde, Dave Thomas dijo:
I know there is a better way to do something like this in PHP:

if (($color == "red") || ($color == "blue") || ($color == "white") ||
($color == "yellow")) { .. }

Is there a way I can condense that so I don't have to write $color
everytime?


,----
| // can also be loaded from somewhere else, say, a db or file
| $available_colo rs = array('red', 'blue', 'white', 'yellow');
|
| if (in_array($colo r, $available_colo rs)) {
| ...
| }
`----

--
Cristian Gutierrez /* cr******@dcc.uc hile.cl */
"Vampirewar e: /n/, a project, capable of sucking the lifeblood out of
anyone unfortunate enough to be assigned to it, which never actually
sees the light of day, but nonetheless refuses to die."
Aug 16 '05 #6
Dave Thomas <ge****@hotmail .com> wrote in news:1dqdnQiykp 9dSJ3eRVn-
iw@adelphia.com:
I know there is a better way to do something like this in PHP:

if (($color == "red") || ($color == "blue") || ($color == "white") ||
($color == "yellow")) { .. }

Is there a way I can condense that so I don't have to write $color
everytime?


You could use a switch statement:
<?php
switch ($color) {
case 'red':
case 'blue':
case 'white':
case 'yellow':
//
// do your stuff here
//
break;
case 'somethingelse' :
echo 'what color is this???';
break;
}
?>

Ken
Aug 16 '05 #7

| Just _what_ are you nattering on about?
| Oh, I see the problem:

so you just jump into this conversation for the sole purpose OF nattering
about HOW someone else posts?!!! don't be a tard! get a good news reader and
you won't have to worry about following the thread. even BETTER, mind your
OWN behavior AND actually post content that addresses the op's concern.
that's more offensive than having someone not summarizing/referencing a
previous post to which he comments.

foad.
Aug 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
4075
by: Robin Becker | last post by:
It seems that the rotor module is being deprecated in 2.3, but there doesn't seem to be an obvious alternative. I'm using it just for obfuscation. It seems we have ssl available in 2.3 for sockets, but there seems no obvious way to use that from python code. Is an alternative to rotor planned? -- Robin Becker
99
4679
by: Paul McGuire | last post by:
There are a number of messages on the python-dev mail list that indicate that Guido is looking for some concensus to come from this list as to what *one* alternative syntax for decorators we would like him to consider in place of the @ syntax that is currently in 2.4a2. I think special thanks are due to: - Anthony Baxter for his continuing efforts in this regard - Steven Bethard for some of the clearest thinking and writing on this topic...
5
12443
by: Colin Steadman | last post by:
I'm looping through an array of employee numbers and sending them to an Oracle procedure for processing. While I'm doing this I'm updating a <DIV> tag in the browser with index number of the current array item so the user knows how far along in processing we are. The ASP looks something like this: <div id=messageToUserTxT>Please wait...</div> <%
2
1615
by: Richard Brooks | last post by:
IE5.5 It's probably an oft repeated question but I have some code that used to work but does not now. This line in my Javascript code give an error now. document.embeds.play(); And the following are the six lines of WAV files that used to be played at
1
13310
by: prasaddevivara | last post by:
I am using the outerHTML property to modify the HTML of existin elements in a web page in Internet Explorer. But same outerHTM property is not working in firefox browser, Anybody can tell me a alternative for outerHTML property in firefox. I am using th following function to display an image and alternate text behind al images of a weg page in Internet Explorer. Anybody can give solutio for same in firefox browser. function...
20
25016
by: Tammy | last post by:
What would be a good alternative to using frames? I need something that will section my webpage into two halves and can change both frames on a single click. Thanks in Advance, Tammy
2
12216
by: rcmail14872 | last post by:
I have seen some very general suggestions for alternative code to the WScript.Shell code, but nothing specific. In the two lines of code below (I think) the first line creates a text file on the hard drive. When I get to this line I get runtime error 429 "ActiveX component can't create object". I know I should not use WSH but I haven't been able to figure out an alternative. I tried looking through the VBA editor help file for "file"...
1
2041
by: Doug Arnott | last post by:
http://www.cs.rutgers.edu/~pxk/rutgers/notes/pdf/Cstyle.pdf has an example at the end of the document of an example of an alternative implementation of interest to me. In this example, the document provides an alternative implementation to the if-else sequence of using strcmp to match a set of strings. Such an example makes me think about the pro's and con's of the different implementations. I am looking for more of such examples. If...
0
2801
by: sachintandon | last post by:
Hello all, Thanks in advance for your help I have a problem in sending emails, my requirement is to send multipart alternative emails with attachments, I'm able to send text with attachments or HTML mails with attachments, but some mail clients are not able to display the html mails that is why I need to send multipart alternative emails, when I used multipart/alternative then I'm not able to send attachments and if I use the multipart/mixed...
11
2306
by: Francine.Neary | last post by:
I've read that as well as "normal" Java-like function definitions, e.g. int main(int argc, char **argv), you can also choose to use an alternative syntax, i.e. int main(argc, argv) int argc; char **argv; { .... }
0
9724
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10394
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7665
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4336
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.