OriginalMacBabe Posted May 25, 2007 Share Posted May 25, 2007 Is it possible to test for more than one value in an "if" statement or must each desired value be checked individually with something like a "switch" or "if-else"? For example, if I'm checking $_SERVER["$HTTP_USER_AGENT"] and I am only interested in selecting for Safari or Opera and all the rest are "else" ...? BONUS question (for me ...) And, can I get a clear answer on the best checker for this: strpos(), strstr(), substr(), or match_preg()?? Thank you OMB Link to comment https://forums.phpfreaks.com/topic/52975-can-i-test-for-more-than-one-value-w-if-or-must-test-individually-w-switch/ Share on other sites More sharing options...
chigley Posted May 25, 2007 Share Posted May 25, 2007 <?php if($variable == "value1" || $variable == "value2") { echo "The variable is either value1 or value2!"; } else { echo "The variable is NOT equal to value1 or value2."; } ?> Link to comment https://forums.phpfreaks.com/topic/52975-can-i-test-for-more-than-one-value-w-if-or-must-test-individually-w-switch/#findComment-261667 Share on other sites More sharing options...
taith Posted May 25, 2007 Share Posted May 25, 2007 a) out of the 4... strpos is the fastest but the others will work also :-) and yes... if(($browser=='opera')||($browser=='safari')){ } Link to comment https://forums.phpfreaks.com/topic/52975-can-i-test-for-more-than-one-value-w-if-or-must-test-individually-w-switch/#findComment-261668 Share on other sites More sharing options...
per1os Posted May 25, 2007 Share Posted May 25, 2007 I use eregi for that type of stuff. www.php.net/eregi strstr can be deceiving in that if what you are looking for starts as the very first part of the string it returns 0, which coincidently also is considered to be false. eregi does not do that and it is case insensitive. Link to comment https://forums.phpfreaks.com/topic/52975-can-i-test-for-more-than-one-value-w-if-or-must-test-individually-w-switch/#findComment-261682 Share on other sites More sharing options...
OriginalMacBabe Posted May 25, 2007 Author Share Posted May 25, 2007 Thank you for the fast replies. <?php if((stripos($_SERVER[“$HTTP_USER_AGENT”], “opera”)) || (stripos($_SERVER[“$HTTP_USER_AGENT”], “safari”))) { include("operasafari.css"); } else { include("otherbrowsers.css"); } ?> Link to comment https://forums.phpfreaks.com/topic/52975-can-i-test-for-more-than-one-value-w-if-or-must-test-individually-w-switch/#findComment-261693 Share on other sites More sharing options...
taith Posted May 25, 2007 Share Posted May 25, 2007 i should mention that including a css file wont do anything ;-) you need your html to link to it ;-) Link to comment https://forums.phpfreaks.com/topic/52975-can-i-test-for-more-than-one-value-w-if-or-must-test-individually-w-switch/#findComment-261700 Share on other sites More sharing options...
chigley Posted May 25, 2007 Share Posted May 25, 2007 ... not if the CSS file has something like this in it: <style type="text/css"> background: #fff; </style> Link to comment https://forums.phpfreaks.com/topic/52975-can-i-test-for-more-than-one-value-w-if-or-must-test-individually-w-switch/#findComment-261703 Share on other sites More sharing options...
OriginalMacBabe Posted May 25, 2007 Author Share Posted May 25, 2007 Caveats duly noted ~ keep the included css as html inline styling. This makes perfect sense since I am only planning on changing 2 lines of css that are browser dependent. In fact, if this is the case, I only need to include ONE new css stylesheets as a result of the "IF" that holds the necessary browser dependent modifications of the otherwise already loaded stylesheet. So, I think the final version of the code will read: <?php if((stripos($_SERVER[“$HTTP_USER_AGENT”], “opera”)) || (stripos($_SERVER[“$HTTP_USER_AGENT”], “safari”))) { include("operasafari.css"); } else { } ?> Thanx Link to comment https://forums.phpfreaks.com/topic/52975-can-i-test-for-more-than-one-value-w-if-or-must-test-individually-w-switch/#findComment-261731 Share on other sites More sharing options...
taith Posted May 25, 2007 Share Posted May 25, 2007 if((stripos($_SERVER[“$HTTP_USER_AGENT”], “opera”)) || (stripos($_SERVER[“$HTTP_USER_AGENT”], “safari”))){ echo '<link rel="stylesheet" type="text/css" href="operasafari.css" />'; } Link to comment https://forums.phpfreaks.com/topic/52975-can-i-test-for-more-than-one-value-w-if-or-must-test-individually-w-switch/#findComment-261734 Share on other sites More sharing options...
OriginalMacBabe Posted May 25, 2007 Author Share Posted May 25, 2007 Is there some reason I can't simply include("inline-styling.css") to override the original stylesheet if the browsers are Opera or Safari? Link to comment https://forums.phpfreaks.com/topic/52975-can-i-test-for-more-than-one-value-w-if-or-must-test-individually-w-switch/#findComment-261794 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.