Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a shorthand way for checking that multiple variables are ALL equal to the same value in an IF statement? (PHP)

Is there a shorthand way of writing the code below?

if (($tstat=='no_prices')&&($l1stat=='no_prices')&&($l2stat=='no_prices')&&($l3stat=='no_prices'))
{                                   
    //do something
}

I tried using the below code,but it did something when one of the variables was not equal to 'no_prices'.

if (($tstat && $l1stat && $l2stat && $l3stat)=='no_prices')
{
    //do something
}

To check that the strings weren't causing problems I tried substituting 0 for 'no_prices' and 1 for other values, but the result was the same.

like image 915
TryHarder Avatar asked Mar 03 '14 07:03

TryHarder


People also ask

How do I check if two variables are equal in PHP?

The comparison operator called Equal Operator is the double equal sign “==”. This operator accepts two inputs to compare and returns true value if both of the values are same (It compares only value of variable, not data types) and return a false value if both of the values are not same.

How do you set multiple variables to the same value?

You can assign the same value to multiple variables by using = consecutively. This is useful, for example, when initializing multiple variables to the same value. It is also possible to assign another value into one after assigning the same value.

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.

What does <> mean in PHP?

The spaceship operator <=> is the latest comparison operator added in PHP 7. It is a non-associative binary operator with the same precedence as equality operators ( == , !=


2 Answers

array_flip is several times faster than array_unique:

function all_equal($arr, $value) {
  return array_keys(array_flip($arr)) == array($value);
}

$arr = array($tstat, $l1stat, $l2stat, $l3stat);

echo all_equal($arr, 'no_prices');

A quick profile for the answers given thus far, for 1000 iterations on array length 1000:

  array_flip: 0.07321620 seconds
array_unique: 0.32569408 seconds
     foreach: 0.15136194 seconds
array_filter: 0.41404295 seconds

The code used to profile is here: http://codepad.org/szgNfWHe

Note: As @cypherabe rightly points out, array_flip does not overtake array_unique until the array has at least 5 elements, and does not overtake foreach until the array has at least 10 elements.

like image 200
primo Avatar answered Sep 26 '22 16:09

primo


Unless I'm mistaken, there's no native way of doing this. If this is something that you have to check for often, try using a custom function for it, e.g.:

function allEquals($chkstr, $vals) {
    if (!is_array($vals)) { die('allEquals() $vals not an array'); }
    foreach ($vals AS $v) {
        if ($chkstr != $v) { return false; }
    }
    return true;
}
like image 34
Flosi Avatar answered Sep 23 '22 16:09

Flosi