This is an old revision of the document!
PHP RFC: Chaining Comparison
- Version: 0.1
- Date: 2016-12-08
- Author: David Walker (dave@mudsite.com), Richard Fussenegger (php@fleshgrinder.com)
- Status: Draft
- First Published at: http://wiki.php.net/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/bp1222/php-src/tree/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
Initial idea on Internals: http://marc.info/?l=php-internals&m=147846422102802&w=2
Rejected Features
Keep this updated with features that were discussed on the mail lists.