PHP Check Value Against Multiple Values with Or-Operator

PHP check value against multiple values with OR-operator

The logical ||(OR) operator doesn't work as you expect it to work. The || operator always evaluates to a boolean either TRUE or FALSE. So in your example your strings get converted into booleans and then compared.

If statement:

if($ext == ('txt' || 'rtf'|| 'log' || 'docx'))

Comes down to:

if($ext == (TRUE || TRUE || TRUE || TRUE))
if($ext == TRUE)

To solve this problem and get the code to work as you want it to you can use different methods.

Multiple comparison

One way to solve the problem and check your values against multiple values is, to actually compare the value against multiple values:

if($ext == "txt" || $ext == "rtf" /* || ... */)

in_array()

Another way is to use the function in_array() and check if the value is equal to one of the array values:

if(in_array($ext, ["txt", "rtf" /* , ... */], TRUE))

Note: Second parameter is for strict comparison

switch()

You could also use switch to check your value against multiple values and just let the case fall through.

switch($ext){

case "txt":
case "rtf":
/* case ...: */
$pClass = "text-";
break;

}

Compare multiple values in PHP

Place the values in an array, then use the function in_array() to check if they exist.

$checkVars = array(3, 4, 5, "string", "2010-05-16");
if(in_array($var, $checkVars)){
// Value is found.
}

http://uk.php.net/manual/en/function.in-array.php

In PHP, is there a short way to compare a variable to multiple values?

in_array() is what I use

if (in_array($variable, array('one','two','three'))) {

Simpler way to check if variable is not equal to multiple string values?

For your first code, you can use a short alteration of the answer given by
@ShankarDamodaran using in_array():

if ( !in_array($some_variable, array('uk','in'), true ) ) {

or even shorter with [] notation available since php 5.4 as pointed out by @Forty in the comments

if ( !in_array($some_variable, ['uk','in'], true ) ) {

is the same as:

if ( $some_variable !== 'uk' && $some_variable !== 'in' ) {

... but shorter. Especially if you compare more than just 'uk' and 'in'.
I do not use an additional variable (Shankar used $os) but instead define the array in the if statement. Some might find that dirty, i find it quick and neat :D

The problem with your second code is that it can easily be exchanged with just TRUE since:

if (true) {

equals

if ( $some_variable !== 'uk' || $some_variable !== 'in' ) {

You are asking if the value of a string is not A or Not B. If it is A, it is definitely not also B and if it is B it is definitely not A. And if it is C or literally anything else, it is also not A and not B. So that statement always (not taking into account schrödingers law here) returns true.

Understanding the logical operator OR in an IF statement

Your usage of your code is wrong. It should be like this.

if ( $category == "apple" || $category == "bananas" || $category == "oranges" ) {

There is a better way to write the code that gives similar results.

if (in_array($category, array("apple", "bananas", "oranges"))) {

OR Condition check multiple values possible?

You're looking for a syntax like the in operator in SQL. But unfortunately for you there is no syntax for that, other than tricks like using in_array, like you suggested yourself.

PHP If Statement with Multiple Conditions

if($var == "abc" || $var == "def" || ...)
{
echo "true";
}

Using "Or" instead of "And" would help here, i think

PHP if statements with multiple values

Just try with:

if ( in_array($_GET['id'], array(1, 3, 4, 5)) ) {}


Related Topics



Leave a reply



Submit