rfc:in_operator

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
rfc:in_operator [2015/02/19 23:56] – updated draft kelunikrfc:in_operator [2015/02/20 00:51] – added traversable, precedence, strict kelunik
Line 1: Line 1:
 ====== PHP RFC: In Operator ====== ====== PHP RFC: In Operator ======
-  * Version: 0.2+  * Version: 0.3
   * Date: 2015-02-20   * Date: 2015-02-20
   * Authors: Niklas Keller <me@kelunik.com>, Bob Weinand <bobwei9@hotmail.com>   * Authors: Niklas Keller <me@kelunik.com>, Bob Weinand <bobwei9@hotmail.com>
Line 7: Line 7:
  
 ===== Introduction ===== ===== Introduction =====
-This RFC adds a new ''in'' operator which simplifies ''contains'' checks for strings and arrays. Currently, we have to use ''in_array($needle, $haystack, true)'' or ''strpos($haystack, $needle) !== false''. These functions have a inconsistent parameter order, so it's hard to remember which is the right one for each. The ''in'' operator makes these checks way more readable.+This RFC adds a new ''in'' operator which simplifies ''contains'' checks for strings and arrays. Currently, we have to use ''in_array($needle, $haystack, true)'' or ''strpos($haystack, $needle) !== false''. These functions have a inconsistent parameter order, so it's hard to remember which is the right one for each. The ''in'' operator makes these checks way more readable. Additionally, it also works for ''Traversable''.
  
 ===== Proposal ===== ===== Proposal =====
 Add a new operator ''(expr1) in (expr2)''. It checks whether ''expr2'' contains ''expr1''. Add a new operator ''(expr1) in (expr2)''. It checks whether ''expr2'' contains ''expr1''.
  
-It uses strict comparison (''==='') for array values and doesn't search recursively.+It uses strict comparison (''==='') for array values / instances of ''Traversable'' and doesn't search recursively.
  
 <code php> <code php>
Line 22: Line 22:
 </code> </code>
  
-If the first parameter is an array, it checks for every element whether it's contained in the ''$haystack'' or not. This is an advantage compared to ''in_array''. It doesn't check the values in ''$needle'' recursively, so it's still possible to check if an array contains another array, but makes checking multiple scalars easier:+<code php> 
 +function gen () { 
 +    yield "foo"; 
 +    yield "bar"; 
 +
 + 
 +$contains = "bar" in gen(); 
 +</code> 
 + 
 +If the first parameter is an array , it checks for every element whether it's contained in the ''$haystack'' or not. This is an advantage compared to ''in_array''. It doesn't check the values in ''$needle'' recursively, so it's still possible to check if an array contains another array, but makes checking multiple scalars easier:
  
 <code php> <code php>
Line 31: Line 40:
 </code> </code>
  
-For stings, it behaves exactly like ''strpos($haystack, $needle) !== false'':+For strings, it behaves exactly like ''strpos($haystack, $needle) !== false'':
 <code php> <code php>
 $contains = "foo" in "foobar"; // true $contains = "foo" in "foobar"; // true
Line 38: Line 47:
  
 Objects are not supported, because we already have ''isset'' here. Objects are not supported, because we already have ''isset'' here.
 +
 +==== Why strict? ====
 +It's strict because otherwise something like ''"foo" in [0]'' would be true.
 +
 +==== Precedence ====
 +It should have the same precedence as ''instanceof'', so it's possible to negate it:
 +
 +<code php>
 +if (!$input in $validValues) {
 +    // ...
 +}
 +</code>
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
rfc/in_operator.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1