rfc:chaining_comparison

This is an old revision of the document!


PHP RFC: Chaining Comparison

Introduction

The point of this RFC is to allow the chaining together of [<, <=, >, >=] comparison operations to allow for easy range checking. Today, to determine if a variable is between two values you end up having to do two operations for the check, rather than a single check.

Today such comparisons must be written as such:

<?php
$a = 10;
 
if (0 < $a && $a < 100) {
    echo "Value is between 0 and 100\n";
}

The proposal of this RFC is to allow new syntax as follows:

<?php
$a = 10;
 
if (0 < $a < 100) {
    echo "Value is between 0 and 100\n";
}

Proposal

Backward Incompatible Changes

This does not break any existing code

Proposed PHP Version(s)

Next PHP (currently 7.2)

RFC Impact

To Opcache

Yes, we're adding new JMP codes when chainging to ensure false values correctly jump over any pre/post inc/dev ops from eval.

Open Issues

Make sure there are no open issues when the vote starts!

Unaffected PHP Functionality

This does not touch the other binary-op comparisons [==, ===, <=>]

Future Scope

Discuss if equality, or spaceship, would need this chaining: '$a == $b == $c' could be possible.

Proposed Voting Choices

Requires 2/3 vote

Patches and Tests

Work In Progress: https://github.com/php/php-src/compare/master...bp1222:multi-compare

Will need eyes of those more familiar with AST/VM to review.

For changes affecting the core language, you should also provide a patch for the language specification.

Implementation

References

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/chaining_comparison.1481327350.txt.gz · Last modified: 2017/09/22 13:28 (external edit)