64

Current Code:

<?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.

starball
  • 20,030
  • 7
  • 43
  • 238
its_me
  • 10,998
  • 25
  • 82
  • 130
  • You only removed the brackets. Please edit your sample code to reflect what you are actually asking. – CBroe Nov 13 '13 at 09:31
  • 1
    @CBroe See the note? Please let me know if it's still unclear. – its_me Nov 13 '13 at 09:33
  • 1
    What it unclear about _“please edit your __sample code__ to reflect what you are actually asking”_? – CBroe Nov 13 '13 at 09:34
  • @CBroe It didn't make sense over adding a NOTE at the bottom, do I didn't. To be more clear, I've made the edits you suggested. **EDIT:** Also noticed another blunder in the code, which I fixed. – its_me Nov 13 '13 at 09:40
  • First case (&&): `!in_array($some_variable, array('uk', 'in'))`, second case (||): True for _whatever_ value $some_variable has, so the whole condition is superfluous – and therefor can be “simplified” by not making this comparison at all :-) – CBroe Nov 13 '13 at 09:56
  • 1
    @CBroe is right - second condition is always TRUE. But small correction to the first: in sample there's usde '!==', so TRUE as third param to the in_array should be passed. `!in_array($some_variable, array('uk', 'in'), true)` – tiriana Nov 13 '13 at 10:11
  • Thanks @Radomir, third param `true` is of course necessary to replicate the exact same behavior. – CBroe Nov 13 '13 at 10:13
  • I dream with `if( $some_variable !== ('uk' OR 'in'))` and why not `if( $some_variable !== ('uk' OR 'in') AND != null)` without being declared/type EACH time :D – m3nda Oct 15 '14 at 07:12

8 Answers8

136

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.

Andresch Serj
  • 35,217
  • 15
  • 59
  • 101
18

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"; 
}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
4

I like the code used by Habchi in comments

if(!($some_variable === 'uk' || $another_variable === 'in')){//do}
Vitalicus
  • 1,188
  • 13
  • 15
3

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.

Booboo
  • 38,656
  • 3
  • 37
  • 60
1

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;
Matt
  • 9,068
  • 12
  • 64
  • 84
Fyntasia
  • 1,133
  • 6
  • 19
1

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}
?>
Michaël Azevedo
  • 3,874
  • 7
  • 31
  • 45
illeas
  • 300
  • 3
  • 18
1

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
}
mranderson
  • 11
  • 1
  • 5
    Having an empty clause condition is not very readable code. Use a ! on your first if statement something like if( !($some_variable === 'uk' || $another_variable === 'in')){ //this occurs when neither of the above are true}. – Habchi Apr 03 '20 at 12:02
-1

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

if(!preg_match('/^uk|in$/', $some_variable)) {
    // Do something
}
Mystical
  • 2,505
  • 2
  • 24
  • 43