Testing for one of a list of values.
Question [PHP] When you have a string of conditions in an 'if' clause and one side of the conditional expression is the same in each condition, is there any shortcut to avoid writing out every expression in full. For examples, rather than writing
if(($name!='Lisa')&&($name!='Leah')&&($name!='Christine')) {
can I write
if($name!=('Lisa'||'Leah'||'Christine')) {
Answer The short answer is "no - you must write it all out if you're using the != operator". In Perl 6 (different language) you WILL have a shortened syntax for this sort of thing but that's highly unusual. But lets' take a wider look at other alternatives.
In php you could write
if (!ereg ('^(Lisa|Leah|Christine)$,$name)) { ....
although that would be slighly less efficient at run time.
I do worry about hardcoding data such as people's names into a program. When Lisa decides she now wants to be called by a different name, or when Chloe joins the team, you're back to the code! Your code would be more maintainable if you had an array of names in a separate file. You could compromise with something like:
$people = array("Hermione","Bob","Sally");
if (in_array($name,$people)) { ...
Note - if you're reading your data into $name from a MySQL database, the
INoperator used in a
WHERE clause will allow you to filter your data before it ever reaches your PHP variables.
(written 2007-05-22, updated 2010-05-15)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H106 - PHP - Arrays [409] Functions and commands with dangerous names - (2005-08-11)
[603] PHP - setting sort order with an associative array - (2006-02-13)
[773] Breaking bread - (2006-06-22)
[832] Displaying data at 5 items per line on a web page - (2006-08-14)
[1116] PHP adding arrays / summing arrays - (2007-03-23)
[1451] More PHP sample and demonstration programs - (2007-12-01)
[1614] When an array is not an array - (2008-04-17)
[2215] If nothing, make it nothing. - (2009-06-02)
[2274] PHP preg functions - examples and comparision - (2009-07-08)
[2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
[2920] Sorting - naturally, or into a different order - (2010-08-14)
[3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
[3379] Sorting data the way YOU want it sorted - (2011-08-05)
[3534] Learning to program in PHP - Regular Expression and Associative Array examples - (2011-12-01)
[4068] Arrays in PHP - contain different and even mixed data types - (2013-04-24)
[4072] Splitting the difference with PHP - (2013-04-27)
[4244] Disambiguation - PHP List - (2014-03-07)
H104 - PHP - Control Statements [340] Code and code maintainance efficiency - (2005-06-08)
[353] Wimbledon Neck - (2005-06-20)
[406] Assignment, equality and identity in PHP - (2005-08-08)
[421] Don't repeat code - use loops or functions - (2005-08-21)
[657] The ternary operator in Python - (2006-03-25)
[863] Double and Triple equals operator in PHP - (2006-09-12)
[962] Breaking a loop - Ruby and other languages - (2006-12-03)
[1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1477] Decisions - small ones, or big ones? - (2007-12-18)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[1825] Question Mark - Colon operator (Perl and PHP) - (2008-10-08)
[2304] Extracting real data from an exported file in PHP or Perl - (2009-07-25)
[2912] Predictions for the seagull population - (2010-08-09)
[3200] How a for loop works Java, Perl and other languages - (2011-03-12)
[3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[3895] Flowchart to program - learning to program with Well House - (2012-10-14)
[3914] While, for, foreach or something else to loop. - (2012-11-06)
[4322] Learning to Program - the conditional statement (if) - (2014-11-21)
[4323] Learning to program - Loop statements such as while - (2014-11-22)
Some other Articles
A Fresh horseReturning multiple values from a function (Perl, PHP, Python)No switch in PythonTraining information - England, Scotland, Wales and IrelandTesting for one of a list of values.From Web to Web 2Back off home with our best wishesThe last tree to leafRegular Express PrimerDrawing hands on a clock face - PHP