Re: problem with foreach

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 10/22/07, Nathan Hawks <nhawks@xxxxxxxxx> 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/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux