Afficher un message
Vieux 22/10/2007, 21h37   #2
Greg Donald
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] problem with foreach

On 10/22/07, Nathan Hawks <nhawks@gmail.com> wrote:
> Also, AFAIK, PHP has always thrown a warning or notice if you use
> foreach() on a non-associative array.


PHP arrays are always associative:

> cat array.php

#!/usr/bin/env php
<?php

error_reporting( E_ALL );

$a = array( 1, 2, 3 );
$b = array( 'a', 'b', 'c' );
$c = array( 1, '2', 'x' );

print_r( $a );
print_r( $b );
print_r( $c );


> ./array.php

Array
(
[0] => 1
[1] => 2
[2] => 3
)
Array
(
[0] => a
[1] => b
[2] => c
)
Array
(
[0] => 1
[1] => 2
[2] => x
)


> That doesn't mean it won't work,
> but the strict standard is apparently:
>
> foreach ($assoc as $key => $val)
>
> not
>
> foreach ($indexed as $item)


What strict standard? The manual shows either use being legal:

http://php.net/foreach

> However, as we all know, the latter still works fine.
>
> Despite the warning/notice you see, the code is probably still working
> as expected (any other problems aside). If you like to keep error


What warning/notice? What other problems?

> reporting on, you can just use an @ to suppress that error.


Errors should be handled not suppressed.


--
Greg Donald
http://destiney.com/
  Réponse avec citation
 
Page generated in 0,07800 seconds with 9 queries