bgbs Posted June 20, 2012 Share Posted June 20, 2012 I have this code <?php if ( $page == "home" ) { echo '<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script><script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/expand.js"></script>';} ?> for $page, can I put several pages in the quotes where I currently list "home". If I can, how would I do this, because putting comma doesn't seem to work for me, although syntax error is not displayed. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/264480-php-if-statement-help/ Share on other sites More sharing options...
HDFilmMaker2112 Posted June 20, 2012 Share Posted June 20, 2012 I have this code <?php if ( $page == "home" ) { echo '<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script><script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/expand.js"></script>';} ?> for $page, can I put several pages in the quotes where I currently list "home". If I can, how would I do this, because putting comma doesn't seem to work for me, although syntax error is not displayed. Thanks in advance You need the "or" separator. You can use the literal "or" or the symbol based version "||" (two straight lines aka pipe character.). I personally prefer the pipe character. http://us.php.net/manual/en/language.operators.logical.php As an example, both of the additions I made are valid. You can pick which ever you prefer, the literal "or" is generally used as an all caps to make it easier to see at a glance in the condition. <?php if ( $page == "home" OR $page=="contact" || $page=="about" ) { echo Quote Link to comment https://forums.phpfreaks.com/topic/264480-php-if-statement-help/#findComment-1355388 Share on other sites More sharing options...
Psycho Posted June 20, 2012 Share Posted June 20, 2012 Another alternative is to use an array and the in_array() function. If you will use the "list" of pages for several things this would probably be a better approach. You can define the array once and the reuse it for whatever you need. $pages = array('home', 'contact', 'about'); if(in_array($page, $pages)) { //Do something } Quote Link to comment https://forums.phpfreaks.com/topic/264480-php-if-statement-help/#findComment-1355390 Share on other sites More sharing options...
bgbs Posted June 20, 2012 Author Share Posted June 20, 2012 You guys are an unbelievable support community. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/264480-php-if-statement-help/#findComment-1355535 Share on other sites More sharing options...
Mahngiel Posted June 20, 2012 Share Posted June 20, 2012 You guys are an unbelievable support community. Thanks Like ! Quote Link to comment https://forums.phpfreaks.com/topic/264480-php-if-statement-help/#findComment-1355537 Share on other sites More sharing options...
RobertP Posted June 20, 2012 Share Posted June 20, 2012 Another alternative is to use an array and the in_array() function. If you will use the "list" of pages for several things this would probably be a better approach. You can define the array once and the reuse it for whatever you need. $pages = array('home', 'contact', 'about'); if(in_array($page, $pages)) { //Do something } this is the way to do it. Quote Link to comment https://forums.phpfreaks.com/topic/264480-php-if-statement-help/#findComment-1355539 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.