Take the State of PHP 2026 Survey →

In Operator View as Markdown

Declined
Authors
Niklas Keller me@kelunik.com, Bob Weinand bobwei9@hotmail.com
Date
2015-03-15
Version
0.5.1
Source
wiki.php.net/rfc/in_operator

Vote

Introduce the in operator? closed
Yes
21
No
26
47 voters
aharvey No 2015-03-17 00:16
ajf No 2015-03-15 21:20
ben Yes 2015-03-18 13:26
bishop No 2015-03-17 01:33
brandon Yes 2015-03-16 15:42
bwoebi Yes 2015-03-15 21:08
damz No 2015-03-16 11:22
danack No 2015-03-15 20:36
duodraco Yes 2015-03-17 12:23
eliw Yes 2015-03-17 12:48
fa No 2015-03-16 08:37
fibbarth Yes 2015-03-23 19:48
galvao No 2015-03-28 02:59
indeyets Yes 2015-03-16 06:16
irker No 2015-03-30 11:20
jedibc Yes 2015-03-16 09:31
jwage Yes 2015-03-15 22:05
kinncj No 2015-03-23 16:24
klaussilveira Yes 2015-03-26 20:31
leigh No 2015-03-21 10:37
lstrojny Yes 2015-03-27 18:46
mbeccati No 2015-03-16 12:06
mike No 2015-03-16 19:00
mj No 2015-03-19 10:34
ocramius No 2015-03-16 00:13
patrickallaert No 2015-03-19 14:59
pauloelr Yes 2015-03-16 02:33
philstu Yes 2015-03-16 00:58
pollita No 2015-03-15 21:45
ralphschindler No 2015-03-16 02:35
ramsey No 2015-03-23 21:45
rdohms Yes 2015-03-19 13:05
rmf Yes 2015-03-18 00:20
salathe No 2015-03-16 08:56
santiagolizardo Yes 2015-03-17 11:54
sebastian No 2015-03-17 06:46
seld Yes 2015-03-15 22:31
stas No 2015-03-15 21:02
stelianm No 2015-03-17 19:50
subjective No 2015-03-30 10:09
theseer Yes 2015-03-25 22:52
till Yes 2015-03-20 20:25
timo No 2015-03-16 15:12
wiesemann Yes 2015-03-17 21:16
yohgaki No 2015-03-19 11:21
yunosh Yes 2015-03-16 16:47
zeev No 2015-03-15 23:58

Discussions on the mailing list

[RFC][Discussion] In Operator
by Niklas Keller · 11 messages
[RFC][DISCUSSION] Context Sensitive Lexer v0.4
by Marcio Almada · 1 message
[RFC][PRE-VOTE] In Operator
by Niklas Keller · 17 messages
[RFC][VOTE] In Operator
by Niklas Keller · 12 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:52 UTC

Introduction

This RFC adds a new in operator which simplifies contains checks for strings and arrays. The in operator makes these checks way more readable and lowers the cognitive load. Additionally, it also works for Traversable.

Motivation

Checking if a specific input in an allowed range of value is a very common check in web application, therefore this operator simplifies those checks (and besides makes them a little bit faster). 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. Additionally, omitting the third parameter for in_array is very common which led to security vulnerabilities in the past.

Proposal

Add a new operator (expr1) in (expr2). It checks whether expr2 contains expr1.

It uses strict comparison (===) for array values / instances of Traversable and doesn't search recursively.

$contains = "foo" in ["a", "b", "c"]; // false
$contains = "foo" in ["foo", "bar"]; // true
$contains = "foo" in [["foo"], ["bar"]]; // false
$contains = "0e0" 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

Traversables are only iterated until there's a match.

function gen () {
    yield "foo";
    yield "bar";
    // code below here wouldn't be executed if "bar" matches
    // because it stops if there's a match.
}
 
$contains = "bar" in gen(); // true
$contains = "baz" in gen(); // false

If $haystack is a string, it's a simple contains check.

$contains = "foo" in "foobar"; // true
$contains = "php" in "foobar"; // false

Other expressions than mixed in array|Traversable or string in string throw an EngineException.

Why strict?

It's strict because otherwise something like “foo” in [0] would pass.

Precedence

It should have the same precedence as instanceof, so it's possible to negate it:

if (!$input in $validValues) {
    // ...
}

Backward Incompatible Changes

New reserved keyword in. This affects function, constant and class, but not class constant and method names, because it depends on the context sensitive lexer being merged.

Proposed PHP Version(s)

Next major release, at the time of writing PHP 7.

RFC Impact

New Constants

A T_IN constant for use with ext/tokenizer has been added.

Future Scope

There could be a syntax that allows to check for multiple values at once, e.g.

$contains = ...["foo", "bar"] in ["foo", "baz", "bar"];

Votes

Requires a 2/3 majority. Even if it passes, it will only get merged if the context sensitive lexer gets merged.

Vote: Introduce the in operator? — Yes: 21 · No: 26 (closed)

Voting started on 2015-03-15 and ends on 2015-03-29.

Patches and Tests

Rejected Features

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

Changelog

  • v0.5: Removed integer support, so the strictness is consistent.
  • v0.4: Removed possibility to check multiple values using an array.