Re: problem with foreach

From: Date: Mon, 22 Oct 2007 19:37:36 +0000
Subject: Re: problem with foreach
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-263518@lists.php.net to get a copy of this message
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/

« previous php.general (#263518) next »