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/20 00:51] – added traversable, precedence, strict kelunikrfc:in_operator [2015/02/26 19:40] kelunik
Line 1: Line 1:
 ====== PHP RFC: In Operator ====== ====== PHP RFC: In Operator ======
-  * Version: 0.3 +  * Version: 0.4 
-  * Date: 2015-02-20+  * Date: 2015-02-26
   * Authors: Niklas Keller <me@kelunik.com>, Bob Weinand <bobwei9@hotmail.com>   * Authors: Niklas Keller <me@kelunik.com>, Bob Weinand <bobwei9@hotmail.com>
-  * Status: Draft+  * Status: Under Discussion
   * First Published at: http://wiki.php.net/rfc/in_operator   * First Published at: http://wiki.php.net/rfc/in_operator
  
Line 20: Line 20:
 $contains = "0e0" in ["0"]; // false, because of strict comparison $contains = "0e0" in ["0"]; // false, because of strict comparison
 $contains = 0 in ["0"]; // false, because of strict comparison $contains = 0 in ["0"]; // false, because of strict comparison
 +
 +$contains = ["foo"] in [["foo"], ["bar"]]; // true
 +$contains = ["foo"] in ["foo"]; // false
 </code> </code>
 +
 +''Traversable''s are only iterated until there's a match.
  
 <code php> <code php>
Line 26: Line 31:
     yield "foo";     yield "foo";
     yield "bar";     yield "bar";
 +    // code below here wouldn't be executed if "bar" matches
 +    // because it stops if there's a match.
 } }
  
-$contains = "bar" in gen();+$contains = "bar" in gen(); // true 
 +$contains = "baz" in gen(); // false
 </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'check the values in ''$needle'' recursivelyso it's still possible to check if an array contains another array, but makes checking multiple scalars easier: +If ''$haystack'' is a string or integer, it's a simple ''contains'' check, integers are converted to their string representation:
- +
-<code php> +
-$contains = ["foo", "bar"] in ["foo", "baz", "bar"]; // true +
-$contains = ["foo", "bar", "baz"] in ["foo", "bar"]; // false +
-$contains = [["foo", "bar"]] in [["foo", "bar"], ["foo", "baz"]]; // true +
-$contains = [["foo", "bar"]] in ["foo", "bar"]; // false +
-</code> +
- +
-For strings, it behaves exactly like ''strpos($haystack, $needle) !== false'':+
 <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>
  
-Objects are not supportedbecause we already have ''isset'' here.+**Note**: This is a difference compared to ''strpos(stringint)'' as it uses the string representation instead of searching for the the ordinal value of a character. 
 + 
 +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? ====
-It's strict because otherwise something like ''"foo" in [0]'' would be true.+It's strict because otherwise something like ''"foo" in [0]'' would pass.
  
 ==== Precedence ==== ==== Precedence ====
Line 61: Line 66:
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
-New reserved keyword ''in''. This affects function, class and method names.+New reserved keyword ''in''. This affects function, constant, class and method names.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 71: Line 76:
  
 ===== Open Issues ===== ===== Open Issues =====
-Make sure there are no open issues when the vote starts!+None.
  
 ===== Future Scope ===== ===== Future Scope =====
-None.+There could be a syntax that allows to check for multiple values at once, e.g. 
 +<code php> 
 +$contains = ...["foo", "bar"] in ["foo", "baz", "bar"]; 
 +</code>
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
Line 80: Line 88:
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
-TBD +  * https://github.com/php/php-src/pull/1121
- +
-===== Implementation ===== +
-TBD+
  
 ===== Rejected Features ===== ===== Rejected Features =====
 Keep this updated with features that were discussed on the mail lists. Keep this updated with features that were discussed on the mail lists.
 +
 +===== Changelog =====
 +  * 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