Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/264480-php-if-statement-help/
Share on other sites

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 

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
}

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.