Jump to content

[SOLVED] or condition in if statement


jbingman

Recommended Posts

im trying to put in content but restrict it to certain pages. The index page, the about us page, and the contact page. I use:

if($_GET['page'] == 'index' || 'about' || 'contact') {
//content here
}elseif($_GET['page'] == 'videos') {
//content here
}elseif($_GET['page'] == 'graphics') {
//content here
}elseif($_GET['page'] == 'web') {
//content here

but that doesnt work. it puts that content in ALL the pages. It works if i take out the other 2 'or' statements in the first if. (the "|| 'about' || 'contact'")

 

Does anyone know why? or how to make that work? thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/83772-solved-or-condition-in-if-statement/
Share on other sites

You need to use

<?php
if($_GET['page'] == 'index' || $_GET['page'] == 'about' || $_GET['page'] == 'contact') {
?>

 

But I would use a switch statement:

<?php
if (isset($_GET['page']))
   switch ($_GET['page']) {
       case 'index':
       case 'about':
       case 'contact':
//
//       code
//
          break;
       case 'videos':
//
//       code
//
          break;
       case 'graphics':
//
//       code
//
          break;
      case 'web':
//
//       code
//
          break;
      default:
//
//       default code
//
          break;
    }
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.