4

Is there a shorter way of writing this (without using regex or string-matching functions)?

if($page=='page1.php' || $page=='page2.php' || $page=='page3.php' || $page=='page4.php'){ do something...} 

I'm looking for something like:

if($page==('page1.php', 'page2.php', 'page3.php', 'page4.php')){do something...} 

but I know that isn't correct. Any suggestions?

5
  • I'm assuming that the values aren't actually page1.php,page2.php etc and they are placeholders? Or is this actually what your code will be checking for? Commented Jan 18, 2012 at 22:26
  • possible duplicate of Compare multiple values in PHP Commented Jan 18, 2012 at 22:26
  • yep...pretty close to a duplicate...I looked for 5 min and couldn't find a question similar to mine...so I posted it. Commented Jan 18, 2012 at 22:28
  • in any case, I think that as with the other one, in_array may well be the best solution Commented Jan 18, 2012 at 22:30
  • See the newest answer to the question Mario links to, on using associative array keys. in_array must search through the array looking for a match (O(n) time complexity), while looking up a string as a key rather than a value takes a constant time to lookup (O(1) time complexity). Commented Jan 19, 2012 at 10:43

5 Answers 5

14

Try in_array:

if (in_array($page, array('page1.php', 'page2.php', 'page3.php'))) { ... } 

http://php.net/manual/en/function.in-array.php

Sign up to request clarification or add additional context in comments.

3 Comments

Maybe I left out the last parens as an exercise for the reader.
well in that case, perfecto! ;)
I just wanted to add a note for posterity. PHP version 5.4+ has a new array syntax so this can be made even (slightly) shorter. Like this: if (in_array($page, ['page1.php', 'page2.php', 'page3.php'])) { ... }
3

Use switch, more readable than a complex if condition

switch ($page){ case 'page1.php': case 'page2.php': case 'page3.php': case 'page4.php': // do something break; default: //else } 

2 Comments

better...but still a little verbose. I'd like to try not to repeat any command more than once.
I think any other option would be less efficient than an if or switch statement.
1

To have an answer that is not same old same old:

if (preg_match('"^page[1-4]\.php$"', $page)) { 

Now this makes sense for your synthetic example, and if you really have ranges of something to test against, or some other structure to go by. Mostly it just happens to be compacter then.

1 Comment

Please don't bug me with microoptimization conjectures if you haven't profiled even the hypothetical example.
0

I think one possible solutions is writing function that as arguments takes page1.php, page2.php etc. and return true if statement is correct.

Comments

0

UPDATE

Sorry for the brain dead answer .. missed the first line. as stated above you could build an array of pages and user in_array()

$pagelist = array('page1.php','page2.php','page3.php','page4.php','page5.php') if (in_array($page,$pagelist)) { //do something } 

it's a bit more elegant and definately cleans up the if statement, but doesn't do much to reduce the code. the only benefit i can think is that you could build the $pagelist array from an external source and using it might be more efficient?

2 Comments

you do realise he already has that exact code at the top of his post - thats what he's trying to avoid!
how is this any different from my question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.