DamienRoche Posted February 18, 2009 Share Posted February 18, 2009 What I want to know is if there is another way to do this: if($var == "1" || $var == "12"){ etc etc } Is there not a way of saying if $var is this or this or this without having to declare the var every time? like: if($var == "1","12","34"){ } Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/145788-solved-efficiency-is-there-a-simpler-operator-in-if-declaration/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 18, 2009 Share Posted February 18, 2009 The only other way of checking if a variable matches one of many values is to use an array and the in_array() statement - http://us.php.net/inarray Quote Link to comment https://forums.phpfreaks.com/topic/145788-solved-efficiency-is-there-a-simpler-operator-in-if-declaration/#findComment-765457 Share on other sites More sharing options...
The Little Guy Posted February 18, 2009 Share Posted February 18, 2009 Well, you could create an array and do that: $arr = array("1","12","34"); if(in_array($var, $arr)){ // Good }else{ // Bad } Quote Link to comment https://forums.phpfreaks.com/topic/145788-solved-efficiency-is-there-a-simpler-operator-in-if-declaration/#findComment-765458 Share on other sites More sharing options...
DamienRoche Posted February 18, 2009 Author Share Posted February 18, 2009 I see, thank you very much. That will come in handy in a few other places. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/145788-solved-efficiency-is-there-a-simpler-operator-in-if-declaration/#findComment-765459 Share on other sites More sharing options...
kenrbnsn Posted February 18, 2009 Share Posted February 18, 2009 There is one other way ... use a switch statement: <?php switch ($var) { case '1': case '12': case '34': // // do your work // break; }?> Probably overkill here, but it is an option. Ken Quote Link to comment https://forums.phpfreaks.com/topic/145788-solved-efficiency-is-there-a-simpler-operator-in-if-declaration/#findComment-765462 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.