par
Ripat » 23 févr. 2006, 21:44
En résumé:
- PHP 3 chaine[$i] OK
- PHP 4 chaine{$i} recommandé, chaine[$i] obsolète
- PHP 5.1 chaine{$i} génère une erreur E_STRICT, chaine[$i] recommandé
- PHP 6 chaine[$i] OK, chaine{$i} interdit
C'est fou ce qu'on s'amuse hein?
Donc, si vous pensez que vos scripts vont survivre au delà de PHP 5 --> chaine[$i]
Liens:
http://fr.php.net/manual/fr/language.types.string.php
http://blog.iworks.at/?/archives/30-Tim ... d-Bye.html
curly braces = accolades
Et voici la raison expliquée par Rasmus Lerdorf himself:
Very few people converted to using {} so the argument about reading old
code doesn't really hold. If you go and grep through all the public
code out there, pretty much none of it uses {} for character offsets.
And internally there is absolutely no difference between {} and [].
Having two syntaxes for the same thing makes no sense, and getting rid
of [] would break all sorts of stuff. The original reason for the {}
was a technical one to simplify the parser, but the landscape has
changed and that reason no longer exists.
As far a code readability and obviousness goes, I doubt anybody would
guess their way to the $str{5} syntax. If you were new to PHP and you
were going to try to guess how you would get a character offset in a
string, what would your first guess be? Most non-PHP people I have
asked have answered []. Removing the obvious syntax just doesn't make
any sense. The other place {} is used outside of control blocks is in
quoted strings where "{$foo{1}}" is much uglier than "{$foo[1]}".
[quote="Ryle"]Petite question parallèle suite au edit de Mario : dans ce topic [url]http://www.phpfrance.com/forums/voir_reponse-102957.php#102957[/url], c'est le contraire qui est indiqué concernant les [] et les {} ...
Quelqu'un peut-il me dire qui a raison et lequel il vaut mieux utiliser ?[/quote]
En résumé:
[list]
[*]PHP 3 chaine[$i] OK
[*]PHP 4 chaine{$i} recommandé, chaine[$i] obsolète
[*]PHP 5.1 chaine{$i} génère une erreur E_STRICT, chaine[$i] recommandé
[*]PHP 6 chaine[$i] OK, chaine{$i} interdit[/list]
C'est fou ce qu'on s'amuse hein? :wink:
Donc, si vous pensez que vos scripts vont survivre au delà de PHP 5 --> chaine[$i]
Liens:
http://fr.php.net/manual/fr/language.types.string.php
http://blog.iworks.at/?/archives/30-Time-to-say-Good-Bye.html
curly braces = accolades
Et voici la raison expliquée par Rasmus Lerdorf himself:
[quote]
Very few people converted to using {} so the argument about reading old
code doesn't really hold. If you go and grep through all the public
code out there, pretty much none of it uses {} for character offsets.
And internally there is absolutely no difference between {} and [].
Having two syntaxes for the same thing makes no sense, and getting rid
of [] would break all sorts of stuff. The original reason for the {}
was a technical one to simplify the parser, but the landscape has
changed and that reason no longer exists.
As far a code readability and obviousness goes, I doubt anybody would
guess their way to the $str{5} syntax. If you were new to PHP and you
were going to try to guess how you would get a character offset in a
string, what would your first guess be? Most non-PHP people I have
asked have answered []. Removing the obvious syntax just doesn't make
any sense. The other place {} is used outside of control blocks is in
quoted strings where "{$foo{1}}" is much uglier than "{$foo[1]}".[/quote]