# Comparison chain

- Thread: https://externals.io/message/79441
- Original archive: https://news-web.php.net/php.internals/79441
- Started by freax@pop3.ru on 2014-12-05T14:36:57+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #79441 — freax@pop3.ru — 2014-12-05T14:36:57+00:00

URL: https://externals.io/message/79441#79441

Hi!

I followed by https://wiki.php.net/rfc/howto and I would like to propose a concept "comparison chain" to implementing to future PHP versions.
It would be very usable if PHP can do that in python-style.

Thank for your attension!

---

## #79444 — Andrea Faulds — 2014-12-05T14:47:49+00:00

URL: https://externals.io/message/79441#79444 · In reply to: #79441

> 
> 
> Hi!
> 
> I followed by https://wiki.php.net/rfc/howto and I would like to propose a concept "comparison chain" to implementing to future PHP versions.
> It would be very usable if PHP can do that in python-style.
> 
> Thank for your attension!

Are you saying to allow (1 < x < 2) etc.? That would be nice, although it’s a backwards-compatibility break as currently (1 < x < 2) would do something different, as would, say, ($x == $y == $z).

--
Andrea Faulds
http://ajf.me/

---

## #79449 — Rowan Collins — 2014-12-05T19:36:02+00:00

URL: https://externals.io/message/79441#79449 · In reply to: #79444

Andrea Faulds wrote on 05/12/2014 14:47:
> > […]
> Are you saying to allow (1 < x < 2) etc.? That would be nice, although it’s a backwards-compatibility break as currently (1 < x < 2) would do something different, as would, say, ($x == $y == $z).

For those of us that don't know Python, an explanation might be handy. 
 From a quick search, this StackOverflow answer sums it up quite nicely, 
assuming it's correct: http://stackoverflow.com/a/25753528/157957

So, "$a < $b < $c" would be short-hand for "$a < $b && $b < $c". It 
would short-circuit in exactly the same way, and be extended to any 
number of comparison operators.

The obvious use case is for maths style "is between" notation, e.g. if ( 
10 < $total <= 100 )

Is this correct, a) for Python, and b) for what is suggested for PHP?

As Andrea notes, it would be a backwards compatibility, although the 
chances of anyone deliberately using a boolean as operand to a < or > 
are unlikely, and that's what's currently implied by $x < $y < $z. 
Perhaps if == was left out of the mix, so that $x == $y == true would 
evaluate the same way it does now, that might be a working compromise?

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #79450 — Nikita Popov — 2014-12-05T19:50:15+00:00

URL: https://externals.io/message/79441#79450 · In reply to: #79444

>
> > […]
> propose a concept "comparison chain" to implementing to future PHP versions.
> > […]
>
> Are you saying to allow (1 < x < 2) etc.? That would be nice, although
> it’s a backwards-compatibility break as currently (1 < x < 2) would do
> something different, as would, say, ($x == $y == $z).
>

Comparisons are non-associative in PHP [1], so something like $x == $y ==
$z is currently invalid code. As such this would not break compatibility.

Nikita

[1]: http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_language_parser.y#81
