Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a shortcut to writing multiple conditions in php?

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?

like image 523
codescribblr Avatar asked Dec 02 '22 00:12

codescribblr


1 Answers

Try in_array:

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

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

like image 164
dkamins Avatar answered Dec 22 '22 20:12

dkamins