# Foreach list behaviour

- Thread: https://externals.io/message/62697
- Original archive: https://news-web.php.net/php.internals/62697
- Started by Jared Williams on 2012-09-02T23:07:49+00:00
- Messages: 11
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #62697 — Jared Williams — 2012-09-02T23:07:49+00:00

URL: https://externals.io/message/62697#62697

Hi,

	Just looking at the foreach list behaviour and it does this...

$i = [1, 2, 3];
foreach($i as list($a, $b))
	var_dump($a, $b);

Outputs....

 `NULL`  `NULL`  `NULL`  `NULL`  `NULL`  `NULL` 

There is no test I can see covering this, so cannot tell if its
expected. 

To me, $i does not meet the expected format, so should be some sort of
error happening.

Jared

---

## #62702 — Laruence — 2012-09-03T02:43:11+00:00

URL: https://externals.io/message/62697#62702 · In reply to: #62697

Hi:
   this is expected behavior,  like:
<?php
list($a, $b) = 1;
var_dump($a);

thanks

On Mon, Sep 3, 2012 at 7:07 AM, Jared Williams
<jared.williams@ntlworld.com> wrote:
> Hi,
>
>         Just looking at the foreach list behaviour and it does this...
>
> $i = [1, 2, 3];
> foreach($i as list($a, $b))
>         var_dump($a, $b);
>
> Outputs....
>
> `NULL` 
> `NULL` 
> `NULL` 
> `NULL` 
> `NULL` 
> `NULL` 
>
> There is no test I can see covering this, so cannot tell if its
> expected.
>
> To me, $i does not meet the expected format, so should be some sort of
> error happening.
>
> Jared
>
>
> --
> 
> 
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #62711 — Derick Rethans — 2012-09-03T08:20:01+00:00

URL: https://externals.io/message/62697#62711 · In reply to: #62702

> 
> > […]
>
>    this is expected behavior,  like:
> <?php
> list($a, $b) = 1;
> var_dump($a);

Ew, that's quite nasty (in both cases). Is there a way how we could turn 
those into a notice or so?

cheers,
Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug
Posted with an email client that doesn't mangle email: alpine

---

## #62712 — Laruence — 2012-09-03T08:36:36+00:00

URL: https://externals.io/message/62697#62712 · In reply to: #62711

> 
>
> > […]
>
> Ew, that's quite nasty (in both cases). Is there a way how we could turn
> those into a notice or so?
Hi:

yeah, we can trigger a notice if the container is not a array.

but maybe deserve  a  RFC?

thanks
>
> cheers,
> Derick
>
> --
> http://derickrethans.nl | http://xdebug.org
> Like Xdebug? Consider a donation: http://xdebug.org/donate.php
> twitter: @derickr and @xdebug
> Posted with an email client that doesn't mangle email: alpine



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #62714 — Pierre Joye — 2012-09-03T09:28:19+00:00

URL: https://externals.io/message/62697#62714 · In reply to: #62712

hi,


> 
> > […]
> Hi:
>
> yeah, we can trigger a notice if the container is not a array.
>
> but maybe deserve  a  RFC?

I do not think it is necessary. It is an improvement of this newly
added feature and list.

Cheers,
-- 
Pierre

@pierrejoye

---

## #62716 — Laruence — 2012-09-03T09:35:32+00:00

URL: https://externals.io/message/62697#62716 · In reply to: #62714

> hi,
>
> 
> > […]
>
> I do not think it is necessary. It is an improvement of this newly
> added feature and list.
Hi:
   if we fixed this,  what about following example:

<?php

$num = NULL;

echo $num["xxx"];


  does this also deserve a notice?

thanks
>
> Cheers,
> --
> Pierre
>
> @pierrejoye



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #62730 — Andrew Faulds — 2012-09-03T12:02:19+00:00

URL: https://externals.io/message/62697#62730 · In reply to: #62716

Laruence <laruence@php.net> wrote:

>Hi:
>   if we fixed this,  what about following example:
>
><?php
>
>$num = NULL;
>
>echo $num["xxx"];
>
>
>  does this also deserve a notice?
>
>thanks

No because `NULL` is just the first element in implicit array conversion
>
>
>
>-- 
>Laruence  Xinchen Hui
>http://www.laruence.com/
>
>-- 
>
>

-- 
Sent from my Android phone with K-9 Mail.
Andrew Faulds
http://ajf.me/

---

## #62758 — Sherif Ramadan — 2012-09-03T17:35:38+00:00

URL: https://externals.io/message/62697#62758 · In reply to: #62711

>
> Ew, that's quite nasty (in both cases). Is there a way how we could turn
> those into a notice or so?
>
> cheers,
> Derick
>

Sorry, hit reply instead of reply-all...

list($a,$b) = 1;
var_dump($a,$b);
/*
 `NULL`  `NULL` 
*/

This doesn't throw notices anywhere else... why should it throw
notices in foreach? I guess the only logical answer would be to notify
you if you were using scalar values with foreach and list, but then we
don't notify you if you're using scalar values with list anywhere else
in the language anyway. I'm starting to see huge inconsistencies with
how list is being implemented in foreach.

---

## #62759 — Andrew Faulds — 2012-09-03T17:36:52+00:00

URL: https://externals.io/message/62697#62759 · In reply to: #62758

> 
> > […]
> Sorry, hit reply instead of reply-all...
>
> list($a,$b) = 1;
> var_dump($a,$b);
> /*
> `NULL` 
> `NULL` 
> */
>
> This doesn't throw notices anywhere else... why should it throw
> notices in foreach? I guess the only logical answer would be to notify
> you if you were using scalar values with foreach and list, but then we
> don't notify you if you're using scalar values with list anywhere else
> in the language anyway. I'm starting to see huge inconsistencies with
> how list is being implemented in foreach.
>
Possibly more importantly, since 1 should cast to, er, [1] (I think...), 
why is $a === NULL?

-- 
Andrew Faulds
http://ajf.me/

---

## #62760 — Sherif Ramadan — 2012-09-03T17:46:32+00:00

URL: https://externals.io/message/62697#62760 · In reply to: #62759

> 
> > […]
> Possibly more importantly, since 1 should cast to, er, [1] (I think...), why
> is $a === NULL?
>
> --
> Andrew Faulds
> http://ajf.me/
>


It should absolute not cast anything. list($a, $b) = $c; is the
equivalent of saying $a = $c[0]; $b = $c[1]; In this case $c just
happens to be a scalar value and as such you can not derive anything
from the scalar value in that context. It is implicitly null.

---

## #62761 — Andrew Faulds — 2012-09-03T17:50:45+00:00

URL: https://externals.io/message/62697#62761 · In reply to: #62760

>
> It should absolute not cast anything. list($a, $b) = $c; is the
> equivalent of saying $a = $c[0]; $b = $c[1]; In this case $c just
> happens to be a scalar value and as such you can not derive anything
> from the scalar value in that context. It is implicitly null.
Ah, I guess I'm confusing GML and PHP array semantics, somehow... my bad.

-- 
Andrew Faulds
http://ajf.me/
