PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Re: [PHP] problem with foreach
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: [PHP] problem with foreach

Réponse
 
LinkBack Outils de la discussion
Vieux 22/10/2007, 20h14   #1
Nathan Hawks
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] problem with foreach

Also, AFAIK, PHP has always thrown a warning or notice if you use
foreach() on a non-associative array. That doesn't mean it won't work,
but the strict standard is apparently:

foreach ($assoc as $key => $val)

not

foreach ($indexed as $item)

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
reporting on, you can just use an @ to suppress that error.
  Réponse avec citation
Vieux 22/10/2007, 20h37   #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
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 15h56.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09222 seconds with 10 queries