69

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.

8
  • You only removed the brackets. Please edit your sample code to reflect what you are actually asking. Commented Nov 13, 2013 at 9:31
  • 1
    @CBroe See the note? Please let me know if it's still unclear. Commented Nov 13, 2013 at 9:33
  • 1
    What it unclear about “please edit your sample code to reflect what you are actually asking”? Commented Nov 13, 2013 at 9: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. Commented Nov 13, 2013 at 9:40
  • 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) Commented Nov 13, 2013 at 10:11

8 Answers 8

144

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.

Sign up to request clarification or add additional context in comments.

4 Comments

For your first answer to be identical, you'll need the third paramter of in_array() set to true to use strict comparison.
You're missing a closing bracket for the in_array call.
Schrödinger reference +1.
As of php 5.4 you can also just use the shorthand [] instead of array().
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"; 
}

5 Comments

And which one of the two code does that simplify? (Please see the edited question)
@CBroe That's the first thing I did. It's still unclear (to me) whether in_array() does && or ||.
“Returns TRUE if needle is found in the array, FALSE otherwise.”
In the above example , uk is the needle and it is compared to all the elements inside the array. Since uk exists.. a match is found.
Same as the accepted answer, you should be using strict comparison in your in_array() check to replicate the OP's if statement
4

I like the code used by Habchi in comments

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

Comments

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.

Comments

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;

Comments

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

Comments

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
}

1 Comment

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

1 Comment

DON'T. USE. REGEX. FOR. EVERYTHING. :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.