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?

codescribblr
  • 1,146
  • 2
  • 11
  • 29
  • 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? – MDEV Jan 18 '12 at 22:26
  • possible duplicate of [Compare multiple values in PHP](http://stackoverflow.com/questions/4106382/compare-multiple-values-in-php) – mario Jan 18 '12 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. – codescribblr Jan 18 '12 at 22:28
  • in any case, I think that as with the other one, in_array may well be the best solution – MDEV Jan 18 '12 at 22:30
  • See the newest answer to the question Mario links to, on using [associative array keys](http://stackoverflow.com/a/4106654/90527). `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). – outis Jan 19 '12 at 10:43

5 Answers5

14

Try in_array:

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

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

dkamins
  • 21,450
  • 7
  • 55
  • 59
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
}
dorsh
  • 23,750
  • 2
  • 27
  • 29
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.

mario
  • 144,265
  • 20
  • 237
  • 291
  • 1
    Please don't bug me with microoptimization conjectures if you haven't profiled even the hypothetical example. – mario Jan 18 '12 at 22:38
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.

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?

Silvertiger
  • 1,680
  • 2
  • 19
  • 32