Hello,
I'd like to propose a new feature to PHP: The in Operator
Bob mentioned a few weeks ago he wants such an operator in PHP and today I
stumbled over
http://nikic.github.io/2012/07/27/How-to-add-new-syntactic-features-to-PHP.html
again,
which uses the in operator as a sample.
Use cases:
$obj = new StdClass;
$obj->prop = "value";
$languages = ["PHP", "C", "Java"];
var_dump("PHP" in $languages); // true
var_dump("Python" in $languages); // false
var_dump(0 in $languages); // false
var_dump("0x0" in ["0e3"]); // false
var_dump("prop" in $obj); // true
var_dump("foo" in $obj); // false
var_dump("Hello" in "Hello World!"); // true
For strings, it would replace strpos("Hello World!", "Hello") !== false)
,
for arrays it would replace in_array($needle, $haystack, true)
and for objects it would be a replacement of property_exists($class,
$property)`.
This change would mainly be syntax sugar, but I think it's worth because:
-
"0x0" in ["0e3"]
would return false whilein_array("0x0", ["0e3"])
returns true, that's probably an edge case, but there may be a lot of
people that don't use the$strict
parameter, see http://3v4l.org/0K7E5 - It would solve the issue that it's hard to remember the order of the
arguments for in_array and strpos, just had to look it up again.
Bob would volunteer to implement that feature.
Best Regards
Niklas Keller
Hello Niklas,
Hello,
I'd like to propose a new feature to PHP: The in Operator
Bob mentioned a few weeks ago he wants such an operator in PHP and today I
stumbled overhttp://nikic.github.io/2012/07/27/How-to-add-new-syntactic-features-to-PHP.html
again,
which uses the in operator as a sample.
I am very familiar with the in operator. However, the implementation
would be incomplete without handling loops via the in operator. Many
people when seeing an in operator also think of JavaScript. In that case
the in operator iterates over properties. As such in PHP we should be able
to iterate over associative arrays should the syntax be added.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
Use cases:
$obj = new StdClass;
$obj->prop = "value";
$languages = ["PHP", "C", "Java"];
var_dump("PHP" in $languages); // true
var_dump("Python" in $languages); // false
var_dump(0 in $languages); // false
var_dump("0x0" in ["0e3"]); // false
var_dump("prop" in $obj); // true
var_dump("foo" in $obj); // false
var_dump("Hello" in "Hello World!"); // trueFor strings, it would replace
strpos("Hello World!", "Hello") !== false)
,
for arrays it would replacein_array($needle, $haystack, true)
and for objects it would be a replacement of property_exists($class,
$property)`.This change would mainly be syntax sugar, but I think it's worth because:
"0x0" in ["0e3"]
would return false whilein_array("0x0", ["0e3"])
returns true, that's probably an edge case, but there may be a lot of
people that don't use the$strict
parameter, see http://3v4l.org/0K7E5- It would solve the issue that it's hard to remember the order of the
arguments for in_array and strpos, just had to look it up again.
I do think that it would be nice for a few different reasons but the in
operator as such can also cause a bit of confusion and misdirection
depending on how it is implemented. In the use cases above you do mention
some inconsistencies with PHP functions that already exist. Additionally
you did mention objects, in what scope would it properly leverage? My
assumption would be visible properties from the context which you are in.
Regards,
Mike
Hi Mike,
I am very familiar with the in operator. However, the implementation
would be incomplete without handling loops via the in operator. Many
people when seeing an in operator also think of JavaScript. In that case
the in operator iterates over properties. As such in PHP we should be able
to iterate over associative arrays should the syntax be added.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
Why? We already have foreach/as which does exactly that, and unlike JS which added for/of, there’s nothing wrong with PHP’s foreach so we don’t need support for a new symbol.
--
Andrea Faulds
http://ajf.me/
Hi Mike,
I am very familiar with the in operator. However, the implementation
would be incomplete without handling loops via the in operator. Many
people when seeing an in operator also think of JavaScript. In that case
the in operator iterates over properties. As such in PHP we should be able
to iterate over associative arrays should the syntax be added.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...inWhy? We already have foreach/as which does exactly that, and unlike JS which added for/of, there’s nothing wrong with PHP’s foreach so we don’t need support for a new symbol.
Indeed, exactly same feature.
Otherwise, well done again Andrea, love it ::)
--
Pierre
@pierrejoye | http://www.libgd.org
2015-01-20 6:35 GMT+01:00 Pierre Joye pierre.php@gmail.com:
Hi Mike,
I am very familiar with the in operator. However, the implementation
would be incomplete without handling loops via the in operator. Many
people when seeing an in operator also think of JavaScript. In that case
the in operator iterates over properties. As such in PHP we should be able
to iterate over associative arrays should the syntax be added.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...inWhy? We already have foreach/as which does exactly that, and unlike JS which added for/of, there’s nothing wrong with PHP’s foreach so we don’t need support for a new symbol.
Indeed, exactly same feature.
Otherwise, well done again Andrea, love it ::)
--
Pierre@pierrejoye | http://www.libgd.org
Hi Mike and Andrea,
In that case the in operator iterates over properties. As such in PHP we should be able to iterate over associative arrays should the syntax be added.
The reason why I didn't include this the reason Andrea mentioned: We
already have foreach
.
My assumption would be visible properties from the context which you are in.
Yes, this is what I'd suggest.
Regards,
Nick
Hello Pierre, Andrea and Niklas,
I am very familiar with the in operator. However, the implementation
would be incomplete without handling loops via the in operator. Many
people when seeing an in operator also think of JavaScript. In that
case
the in operator iterates over properties. As such in PHP we should be
able
to iterate over associative arrays should the syntax be added.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
Why? We already have foreach/as which does exactly that, and unlike JS
which added for/of, there’s nothing wrong with PHP’s foreach so we don’t
need support for a new symbol.Indeed, exactly same feature.
It certainly is the same feature from the foreach perspective. I'm not
against leaving it out, however, it would be much like the in operator thus
providing some additional syntax sugar. Many people that often program in
JavaScript are used to having:
array.forEach(function()), for (property in object), etc.
From my perspective it would seem like a hole in the implementation for it
to be missing but that is also due to having such a split of time between
the two languages. Take for instance python utilizing the same thing:
for variable in expression:
... code
...
as well as ruby:
for variable [, variable ...] in expression [do]
code
end
Ultimately, I would feel that having the in operator would also create the
expectation that in PHP we would be able to achieve the following:
for ($var in $array) {} and/or for ($var in $object) {};
Certainly foreach is more rich but I believe based on other languages and
paradigms that this would be something people would request and expect if
the in operator was to make it into PHP 7.
Regards,
Mike
Hi,
Mike, the use of "in" as for ($var in $object) {};
could be the
subject for another distinct RFC since it's doing something different
from the original proposal:
var_dump("PHP" in ["PHP", "C", "Java"]); // true
2015-01-20 10:41 GMT-03:00 Mike Willbanks pencap@gmail.com:
Hello Pierre, Andrea and Niklas,
I am very familiar with the in operator. However, the implementation
would be incomplete without handling loops via the in operator. Many
people when seeing an in operator also think of JavaScript. In that
case
the in operator iterates over properties. As such in PHP we should be
able
to iterate over associative arrays should the syntax be added.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
Why? We already have foreach/as which does exactly that, and unlike JS
which added for/of, there’s nothing wrong with PHP’s foreach so we don’t
need support for a new symbol.Indeed, exactly same feature.
It certainly is the same feature from the foreach perspective. I'm not
against leaving it out, however, it would be much like the in operator thus
providing some additional syntax sugar. Many people that often program in
JavaScript are used to having:
array.forEach(function()), for (property in object), etc.From my perspective it would seem like a hole in the implementation for it
to be missing but that is also due to having such a split of time between
the two languages. Take for instance python utilizing the same thing:for variable in expression:
... code
...as well as ruby:
for variable [, variable ...] in expression [do]
code
endUltimately, I would feel that having the in operator would also create the
expectation that in PHP we would be able to achieve the following:for ($var in $array) {} and/or for ($var in $object) {};
Certainly foreach is more rich but I believe based on other languages and
paradigms that this would be something people would request and expect if
the in operator was to make it into PHP 7.Regards,
Mike
Hello,
On Tue, Jan 20, 2015 at 7:47 AM, Marcio Almada marcio.web2@gmail.com
wrote:
Hi,
Mike, the use of "in" as
for ($var in $object) {};
could be the
subject for another distinct RFC since it's doing something different
from the original proposal:var_dump("PHP" in ["PHP", "C", "Java"]); // true
It is indeed different but ever so slightly, a for loop in this case cannot
handle a single argument of true and would cause a parse error.
Therefore for ("PHP" in ["PHP, "C", "Java"]) could be handled differently.
Again, my main point is that due to other languages the in operator itself
also correlates to for ($var in $container), I understand having an
additional RFC but this is indeed the place for discussion of such things.
Also FYI - in the future please try not to top post and instead post under
where you are commenting as it makes it more difficult for people coming
later to the thread to see the scope of the conversation.
Regards,
Mike
2015-01-20 10:41 GMT-03:00 Mike Willbanks pencap@gmail.com:
Hello Pierre, Andrea and Niklas,
On Mon, Jan 19, 2015 at 11:35 PM, Pierre Joye pierre.php@gmail.com
wrote:I am very familiar with the in operator. However, the
implementation
would be incomplete without handling loops via the in operator. Many
people when seeing an in operator also think of JavaScript. In that
case
the in operator iterates over properties. As such in PHP we should
be
able
to iterate over associative arrays should the syntax be added.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
Why? We already have foreach/as which does exactly that, and unlike JS
which added for/of, there’s nothing wrong with PHP’s foreach so we don’t
need support for a new symbol.Indeed, exactly same feature.
It certainly is the same feature from the foreach perspective. I'm not
against leaving it out, however, it would be much like the in operator
thus
providing some additional syntax sugar. Many people that often program
in
JavaScript are used to having:
array.forEach(function()), for (property in object), etc.From my perspective it would seem like a hole in the implementation for
it
to be missing but that is also due to having such a split of time between
the two languages. Take for instance python utilizing the same thing:for variable in expression:
... code
...as well as ruby:
for variable [, variable ...] in expression [do]
code
endUltimately, I would feel that having the in operator would also create
the
expectation that in PHP we would be able to achieve the following:for ($var in $array) {} and/or for ($var in $object) {};
Certainly foreach is more rich but I believe based on other languages and
paradigms that this would be something people would request and expect if
the in operator was to make it into PHP 7.Regards,
Mike
Mike,
Also FYI - in the future please try not to top post and instead post under where you are commenting as it makes it more difficult for
people coming later to the thread to see the scope of the conversation.
Thanks for the tip! I read to "do not top post" on the guidelines but
had no clue what it meant until now xD
Mike,
Back to the subject,:
It is indeed different but ever so slightly, a for loop in this case cannot handle a single argument of true and would cause a parse error.
Therefore for ("PHP" in ["PHP, "C", "Java"]) could be handled differently. Again, my main point is that due to other languages the in
operator itself also correlates to for ($var in $container), I understand having an additional RFC but this is indeed the place for
discussion of such things.
Agree, but maybe a RFC for "in" operator and a follow up RFC to allow
"in" on "for" context could be a win/win here as long as one feature
doesn't block each other?
2015-01-20 11:10 GMT-03:00 Marcio Almada marcio.web2@gmail.com:
Mike,
Also FYI - in the future please try not to top post and instead post under where you are commenting as it makes it more difficult for
people coming later to the thread to see the scope of the conversation.Thanks for the tip! I read to "do not top post" on the guidelines but
had no clue what it meant until now xD
Hi!
Agree, but maybe a RFC for "in" operator and a follow up RFC to allow
"in" on "for" context could be a win/win here as long as one feature
doesn't block each other?
The problem is "in" is not just "for". The problem is this: when you say
"$foo in $bar", is $foo a value or a key? In many languages, $foo would
be a value for a list (and a vector) and a key for a map, but in PHP
lists, vectors and maps are the same data type, so you can not implement
it in the same way as, say, in Python. I.e. in Python:
1 in [1,2,3]
True
1 in {1:'a',2:'b',3:'c'}
True
1 in {'a':1, 'b':2, 'c':3}
False
5 in [5,6,7]
True
5 in {0:5,1:6,2:7}
False
In PHP, even if it'd be possible to implement such semantics (which I'm
not sure), it would be very confusing and hard to explain.
--
Stas Malyshev
smalyshev@gmail.com
Mike Willbanks wrote on 20/01/2015 03:30:
I am very familiar with the in operator. However, the implementation
would be incomplete without handling loops via the in operator. Many
people when seeing an in operator also think of JavaScript. In that case
the in operator iterates over properties. As such in PHP we should be able
to iterate over associative arrays should the syntax be added.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
I don't really see how this is related to the in operator, other than
that some languages reuse the same keyword for both purposes.
My immediate association when I saw this proposal was not "ah, like the
loop syntax in JavaScript", but "ah, like the IN() operator in SQL".
Given the ability to write foreach ( $foo as $key => $value ), I'm not
sure adding a variant syntax of for ( $key in $foo ) has that much value
for PHP.
Regards,
Rowan Collins
[IMSoP]
Am 20.01.2015 um 16:42 schrieb Rowan Collins:
Given the ability to write foreach ( $foo as $key => $value ), I'm not
sure adding a variant syntax of for ( $key in $foo ) has that much value
for PHP.
I totally agree with you here.
The only real reason why Javascript has a for-in operation that iterates
over object properties is the lack of non-integer keys in Javascript arrays.
For PHP iterating over object properties is a very rare case IMHO. So I
would stick with foreach for that case.
Kind regards,
Dennis Birkholz
De : Rowan Collins [mailto:rowan.collins@gmail.com]
Given the ability to write foreach ( $foo as $key => $value ), I'm not
sure adding a variant syntax of for ( $key in $foo ) has that much value
for PHP.
+1. Useless and ambiguous.
Mike Willbanks wrote on 20/01/2015 03:30:
I am very familiar with the in operator. However, the implementation
would be incomplete without handling loops via the in operator. Many
people when seeing an in operator also think of JavaScript. In that
case
the in operator iterates over properties. As such in PHP we should
be able
to iterate over associative arrays should the syntax be added.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...inI don't really see how this is related to the in operator, other than
that some languages reuse the same keyword for both purposes.My immediate association when I saw this proposal was not "ah, like
the loop syntax in JavaScript", but "ah, like the IN() operator in SQL".Given the ability to write foreach ( $foo as $key => $value ), I'm not
sure adding a variant syntax of for ( $key in $foo ) has that much
value for PHP.Regards,
I like the idea of a boolean IN. If nothing else it would simplify the
common cases for strpos and in_array()
and friends. I agree that
leaving out for/foreach is a good idea as that's a different thing that
would just so happen to use the same new reserved word. That's fine;
"use" became reserved for namespaces and then was reused for traits
without incident.
However, we should also ask what happens with objects. I've seen a lot
of people trying to push toward collection objects rather than plain
arrays for a variety of reasons, and of course we run smack into the
"PHP has all of these cool array-manipulation functions that are almost
great for functional programming but don't work on iterators, ah crap"
problem.
So what would the following do:
$o = new stdclass;
$o->a = 'b';
$o->c = 'd';
print 'a' in $o;
print 'b' in $o;
Or more to the point:
$a = new ArrayObject(['a', 'b', 'c']);
print 'c' in $a;
There are likely lots of implementation details around making it work,
but it would be lovely if at least some collection objects could support
IN. Perhaps an Innable interface? :-) (Which then an iterator could
also implement if it made logical sense for it to do so, which is not
always the case.)
--Larry Garfield
Hi Niklas,
I'd like to propose a new feature to PHP: The in Operator
Bob mentioned a few weeks ago he wants such an operator in PHP and today I
stumbled overhttp://nikic.github.io/2012/07/27/How-to-add-new-syntactic-features-to-PHP.html
again,
which uses the in operator as a sample.Use cases:
$obj = new StdClass;
$obj->prop = "value";
$languages = ["PHP", "C", "Java"];
var_dump("PHP" in $languages); // true
var_dump("Python" in $languages); // false
var_dump(0 in $languages); // false
var_dump("0x0" in ["0e3"]); // false
var_dump("prop" in $obj); // true
var_dump("foo" in $obj); // false
var_dump("Hello" in "Hello World!"); // trueFor strings, it would replace
strpos("Hello World!", "Hello") !== false)
,
for arrays it would replacein_array($needle, $haystack, true)
and for objects it would be a replacement of property_exists($class,
$property)`.This change would mainly be syntax sugar, but I think it's worth because:
"0x0" in ["0e3"]
would return false whilein_array("0x0", ["0e3"])
returns true, that's probably an edge case, but there may be a lot of
people that don't use the$strict
parameter, see http://3v4l.org/0K7E5- It would solve the issue that it's hard to remember the order of the
arguments for in_array and strpos, just had to look it up again.Bob would volunteer to implement that feature.
I would like to reserve 'in' and 'out' for Design by Contract (DbC).
http://en.wikipedia.org/wiki/Design_by_contract
D language has in and out like
function foo() {
in {
// precondition
}
out {
// postcondition
}
{
// function body
}
Contract programing can be done only with assert or annotation.
However, it's much easier/readable with native language support.
Contract programming is proved to be fast/robust/secure.
in operator can be useful, but it's the same as in_array()
.
If we need strict type check, we may do
$ php -r 'var_dump(in_array("0x0", ["0e3"], TRUE));'
bool(false)
as you mentioned.
I would like to reserve 'in' for DbC rather than syntax sugar.
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net
Yasuo Ohgaki wrote on 23/01/2015 03:01:
Hi Niklas,
I'd like to propose a new feature to PHP: The in Operator
Bob mentioned a few weeks ago he wants such an operator in PHP and today I
stumbled overhttp://nikic.github.io/2012/07/27/How-to-add-new-syntactic-features-to-PHP.html
again,
which uses the in operator as a sample.Use cases:
$obj = new StdClass;
$obj->prop = "value";
$languages = ["PHP", "C", "Java"];
var_dump("PHP" in $languages); // true
var_dump("Python" in $languages); // false
var_dump(0 in $languages); // false
var_dump("0x0" in ["0e3"]); // false
var_dump("prop" in $obj); // true
var_dump("foo" in $obj); // false
var_dump("Hello" in "Hello World!"); // trueFor strings, it would replace
strpos("Hello World!", "Hello") !== false)
,
for arrays it would replacein_array($needle, $haystack, true)
and for objects it would be a replacement of property_exists($class,
$property)`.This change would mainly be syntax sugar, but I think it's worth because:
"0x0" in ["0e3"]
would return false whilein_array("0x0", ["0e3"])
returns true, that's probably an edge case, but there may be a lot of
people that don't use the$strict
parameter, seehttp://3v4l.org/0K7E5- It would solve the issue that it's hard to remember the order of the
arguments for in_array and strpos, just had to look it up again.Bob would volunteer to implement that feature.
I would like to reserve 'in' and 'out' for Design by Contract (DbC).
http://en.wikipedia.org/wiki/Design_by_contractD language has in and out like
function foo() {
in {
// precondition
}
out {
// postcondition
}
{
// function body
}
Would this conflict? It seems to me that this syntax could only ever
exist in a declaration (function signature) context, and the proposed in
operator could only ever exist in a procedural code (function body)
context, which are surely completely separate sections of the parser.
If anything, wanting the keyword for another purpose might strengthen
its case, because each feature isn't reserving a new word. It's not the
first keyword to have distinct meanings by context - "use" means very
different things for namespaces, traits, and closures.
Regards,
Rowan Collins
[IMSoP]