Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to chain comparison operators?

I've been thus far unable to find this information in the official PHP docs, or on this site. So, that may mean I'm searching under the wrong terms, or it is not supported. What am I looking for? I'll describe it...

Let's say I have the following comparisons in PHP:

if (($a == $b) && ($b == $c))
    doSomething();
else
    doSomethingElse();

if (($d < $e) && ($e < $f))
    doSomething();
else
    doSomethingElse();

Does PHP have some kind of syntax to chain the comparisons together without the explicit AND-ing of two different comparisons? For example, is something like this possible:

if ($a == $b == $c)
    doSomething();
else
    doSomethingElse();

if ($d < $e < $f)
    doSomething();
else
    doSomethingElse();

Note that I am looking for a syntactic shorthand in the language. I know that I can easily write a function for each of these chained comparisons, but this is a kludgy work-around, and not desired. For example:

function chainedGreaterThan($args)
{
    for ($i = 0; $i < count($args) - 1; $i++)
        if ($args[$i] <= $args[$i + 1])
            return false;
    return true;
}

This technically would work, but is not a syntactic shorthand given by the language.

like image 432
E.T. Avatar asked Feb 25 '13 15:02

E.T.


People also ask

Can you chain == Python?

Chaining with the comparison operator == returns True only if all values are equal. If there is even one different value, False is returned. Be careful when using the comparison operator != that returns True when the values are not equivalent.

How do you chain operators in Python?

The chaining of operators can be written as follows: if a < b < c : {.....} According to associativity and precedence in Python, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation.

What is chained comparison in Python?

Python supports chaining of comparison operators, which means if we wanted to find out if b lies between a and c we can do a < b < c , making code super-intuitive. Python evaluates such expressions like how we do in mathematics. which means a < b < c is evaluated as (a < b) and (b < c) .


2 Answers

No, PHP doesn't have anything like this.

like image 76
Dogbert Avatar answered Sep 22 '22 10:09

Dogbert


You could do something awful like this when you have very large amounts of things to compare.

<?php
$arr = [1, 2, 3];
$less_than = function($a, $b) {
    return $a < $b;
};
$greater_than = function($a, $b) {
    return $a > $b;
};

function apply_operator($arr, $operator) {
    for ($i = 0; $i < sizeof($arr) - 1; $i++) {
        if (!$operator($arr[$i], $arr[$i + 1])) {
            return false;
        }
    }
    return true;
}

var_dump(apply_operator($arr, $less_than)); // true
var_dump(apply_operator($arr, $greater_than)); // false

But for greater/less than you can just sort and compare to the original, and for equal you can check the size of array_unique.

like image 28
Waleed Khan Avatar answered Sep 20 '22 10:09

Waleed Khan