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 revision
Previous revision
Next revisionBoth sides next revision
rfc:in_operator [2015/02/26 19:40] kelunikrfc:in_operator [2015/03/15 02:04] – change BC changes kelunik
Line 1: Line 1:
 ====== PHP RFC: In Operator ====== ====== PHP RFC: In Operator ======
-  * Version: 0.4 +  * Version: 0.5.1 
-  * Date: 2015-02-26+  * Date: 2015-03-15
   * Authors: Niklas Keller <me@kelunik.com>, Bob Weinand <bobwei9@hotmail.com>   * Authors: Niklas Keller <me@kelunik.com>, Bob Weinand <bobwei9@hotmail.com>
   * Status: Under Discussion   * Status: Under Discussion
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. Additionally, it also works for ''Traversable''.+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 and lowers the cognitive load. Additionally, it also works for ''Traversable''.
  
 ===== Proposal ===== ===== Proposal =====
Line 39: Line 39:
 </code> </code>
  
-If ''$haystack'' is a string or integer, it's a simple ''contains'' check, integers are converted to their string representation:+If ''$haystack'' is a string, it's a simple ''contains'' check
 <code php> <code php>
 $contains = "foo" in "foobar"; // true $contains = "foo" in "foobar"; // true
 $contains = "php" in "foobar"; // false $contains = "php" in "foobar"; // false
-$contains = 0 in "0"; // true 
-$contains = 0 in 100; // true 
 </code> </code>
  
-**Note**: This is a difference compared to ''strpos(string, int)'' as it uses the string representation instead of searching for the the ordinal value of a character. +Other expressions than ''mixed in array|Traversable'' or ''string in string'' throw an ''EngineException''.
- +
-Other values than ''string'', ''integer'', ''array'' or ''Traversable'' for ''$haystack'' will return false and emit a warning. +
- +
-If ''$haystack'' is of type string, only ''string'' and ''integer'' are allowed as type of ''$needle''. Other types will return false and emit a warning.+
  
 ==== Why strict? ==== ==== Why strict? ====
Line 66: Line 61:
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
-New reserved keyword ''in''. This affects function, constant, class and method names.+New reserved keyword ''in''. This affects function, constant and classbut not class constant and method names.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 74: Line 69:
 ==== New Constants ==== ==== New Constants ====
 A ''T_IN'' constant for use with ext/tokenizer has been added. A ''T_IN'' constant for use with ext/tokenizer has been added.
- 
-===== Open Issues ===== 
-None. 
  
 ===== Future Scope ===== ===== Future Scope =====
Line 84: Line 76:
 </code> </code>
  
-===== Proposed Voting Choices ===== +===== Votes ===== 
-Requires a 2/3 majority, simple yes no vote.+Requires a 2/3 majority. Even if it passesit will //only// get merged if the [[rfc/context_sensitive_lexer|context sensitive lexer]] gets merged.
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 94: Line 86:
  
 ===== Changelog ===== ===== Changelog =====
 +  * v0.5: Removed integer support, so the strictness is consistent.
   * v0.4: Removed possibility to check multiple values using an array.   * v0.4: Removed possibility to check multiple values using an array.
rfc/in_operator.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1