-11

I'm using the below statement on my website. I want to be able to say if the selected_plan_id is 4, 5, OR 6. Any of the OR variations I use break my code.

 <?php if($CURRENT_USER['selected_plan_id'] == '5'): ?>

Here's the ones I tried so far:

 <?php if($CURRENT_USER['selected_plan_id'] == '5' or '4' or '6'): ?>

:( error

 <?php if($CURRENT_USER['selected_plan_id'] == '"5", "4", "6"'): ?>

:( displayed this for every user

 <?php if($CURRENT_USER['selected_plan_id'] == '5'): or if($CURRENT_USER['selected_plan_id'] == '4'): or if($CURRENT_USER['selected_plan_id'] == '6'): ?>

:( error

3 Answers3

2

Use || statement.

<?php if($CURRENT_USER['selected_plan_id'] == '4' || $CURRENT_USER['selected_plan_id'] == '5' || $CURRENT_USER['selected_plan_id'] == '6'): ?>

Or

Use in_array().

$arr = Array (4,5,6);
<?php if(in_array($CURRENT_USER['selected_plan_id'],$arr)): ?>
RJParikh
  • 4,096
  • 1
  • 19
  • 36
1
<?php if( ( $CURRENT_USER['selected_plan_id'] == '4') || ( $CURRENT_USER['selected_plan_id'] == '5') || ( $CURRENT_USER['selected_plan_id'] == '6') ) : ?>

This will look to see if the $CURRENT_USER['selected_plan_id'] is 4 or 5 or 6

Blinkydamo
  • 1,582
  • 9
  • 20
  • 3
    ***Please, don't reward bad questions with answers.*** – Epodax Oct 25 '16 at 10:00
  • 2
    From your own profile - "I'm developer and find myself at SO both to **help other people** but also to learn from others, both by asking and by reading other peoples questions." - I see no issue in assisting others. – Blinkydamo Oct 25 '16 at 10:02
  • I help people who follow SO's standard and rule set, there's no effort shown here, further more, this isn't advanced php or complex logic, it's some of the more basic things that can easily be researched. – Epodax Oct 25 '16 at 10:03
  • I disagree it can be easily researched, I've been searching for over an hour and every OR example I've tried with my code does not work. Apologies but I didn't include what I tried as none of it worked (so didn't see the point). Some of the ones I tried are there now – superhappybunnycat Oct 25 '16 at 10:05
  • Unfortunately not everyone will go looking for rules every time they enter a website, they just want to know the answers. – Phiter Oct 25 '16 at 10:06
  • @PhiterFernandes I'm not sure what that's suppose to mean, - superhappybunnycat All of your shown attempts show no usage of the correct syntax, http://stackoverflow.com/questions/4405795/using-and-or-in-if-else-php-statement just one easy found example. – Epodax Oct 25 '16 at 10:10
  • @superhappybunnycat I'd recommend you take a PHP course. Codeacademy provides one with all basic stuff like this one and also you get to know arrays. I'd say it is a very complete basic course. – Phiter Oct 25 '16 at 10:11
0

My instinct would lead me to in_array() because it seems to be a simpler way to express the idea.
Here are the man pages for the "or" operator. http://php.net/manual/en/language.operators.logical.php http://php.net/manual/en/language.operators.precedence.php

Ray Paseur
  • 2,106
  • 2
  • 13
  • 18