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. 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 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 } 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! 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 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
Archived
This topic is now archived and is closed to further replies.