[php] 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.

This question is related to php if-statement conditional-statements

The answer is


If you're planning on building a function in the if statement, I'd also advise the use of in_array. It's a lot cleaner.

If you're attempting to assign values to variables you can use the if/else shorthand:

$variable_to_fill = $some_variable !== 'uk' ? false : true;

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"; 
}

An alternative that might make sense especially if this test is being made multiple times and you are running PHP 7+ and have installed the Set class is:

use Ds\Set;

$strings = new Set(['uk', 'in']);    
if (!$strings->contains($some_variable)) {

Or on any version of PHP you can use an associative array to simulate a set:

$strings = ['uk' => 1, 'in' => 1];
if (!isset($strings[$some_variable])) {

There is additional overhead in creating the set but each test then becomes an O(1) operation. Of course the savings becomes greater the longer the list of strings being compared is.


You may find it more readable to reverse your logic and use an else statement with an empty if.

if($some_variable === 'uk' || $another_variable === 'in'){}

else {
    // This occurs when neither of the above are true
}

Some basic regex would do the trick nicely for $some_variable !== 'uk' && $some_variable !== 'in':

if(!preg_match('/^uk|in$/', $some_variable)) {
    // Do something
}

You need to multi value check. Try using the following code :

<?php
    $illstack=array(...............);
    $val=array('uk','bn','in');
    if(count(array_intersect($illstack,$val))===count($val)){ // all of $val is in $illstack}
?>

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to if-statement

How to use *ngIf else? SQL Server IF EXISTS THEN 1 ELSE 2 What is a good practice to check if an environmental variable exists or not? Using OR operator in a jquery if statement R multiple conditions in if statement Syntax for an If statement using a boolean How to have multiple conditions for one if statement in python Ifelse statement in R with multiple conditions If strings starts with in PowerShell Multiple conditions in an IF statement in Excel VBA

Examples related to conditional-statements

How to have multiple conditions for one if statement in python Excel - programm cells to change colour based on another cell Using OR & AND in COUNTIFS C error: Expected expression before int Conditional Replace Pandas SELECT query with CASE condition and SUM() Simpler way to check if variable is not equal to multiple string values? Replace all elements of Python NumPy Array that are greater than some value How can I check if a string only contains letters in Python? Check if year is leap year in javascript