Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Current Codes:

<?php    // See the AND operator; How do I simplify/shorten this line?   if( $some_variable !== 'uk' && $some_variable !== 'in' ) {      // Do something    }  ?> 

And:

<?php    // See the OR operator; How do I simplify/shorten this line?   if( $some_variable !== 'uk' || $some_variable !== 'in' ) {      // Do something else    }  ?> 

Is there a simpler (i.e. shorter) way to write the two conditions?

NOTE: Yes, they are different, and I am expecting different ways to shorten the codes.

like image 780
its_me Avatar asked Nov 13 '13 09:11

its_me


People also ask

How do you check if a variable is not equal to multiple values?

To check if a variable is not equal to multiple values:Use the logical and (&&) operator to chain multiple conditions. In each condition, use the strict inequality operator (! ==) to check that the variable is not equal to the value. If all conditions pass, the variable is not equal to any of the values.

How do you find if a not equal condition in an if statement?

Equality operators: == and != The result type for these operators is bool . The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false . The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .

How do I test multiple variables in Python?

To test multiple variables x , y , z against a value in Python, use the expression value in {x, y, z} . Checking membership in a set has constant runtime complexity. Thus, this is the most efficient way to test multiple variables against a value.


2 Answers

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.

like image 129
Andresch Serj Avatar answered Sep 19 '22 06:09

Andresch Serj


You can make use of in_array() in PHP.

$os = array("uk", "us"); // You can set multiple check conditions here if (in_array("uk", $os)) //Founds a match ! {     echo "Got you";  } 
like image 30
Shankar Narayana Damodaran Avatar answered Sep 22 '22 06:09

Shankar Narayana Damodaran