Warnung: file_put_contents(/home/www/web1/html/php_dev/test.txt) [function.file-put-contents]: failed to open stream: Permission denied in /home/www/web1/html/php_dev/sys/lib.activity.php (Zeile 58)
Arrays mit Kommata abschliessen? [Archiv] - PHP-Scripte PHP-Tutorials PHP-Jobs und vieles mehr

- Ad -
php-resource




Archiv verlassen und diese Seite im Standarddesign anzeigen :
Arrays mit Kommata abschliessen?


 
Lennynero
20-04-2010, 10:24 
 
Hi,

irgendwann muss ich über Skripte gestolpert sein, bei denen auch hinter dem letzten Element eines Arrays ein Kommata steht:


echo array(1,2,3,);


Mehr oder weniger unbewusst habe ich das auch mit in meine Skripte übernommen und jetzt habe ich mir überlegt, ob das überhaupt richtig so ist oder einfach ein fehlertolerantes Verhalten.

Auf php.net bin ich dann über folgende Formulierung gestolpert:


Similarly to a comment by stlawson at sbcglobal dot net on this page:
PHP: Instruction separation - Manual (http://www.php.net/basic-syntax.instruction-separation)

It is usually advisable to define your arrays like this:
$array = array(
'foo',
'bar',
);

Note the comma after the last element - this is perfectly legal. Moreover,
it's best to add that last comma so that when you add new elements to the
array, you don't have to worry about adding a comma after what used to be
the last element.

<?php
$array = array(
'foo',
'bar',
'baz',
);
?>


PhpEd, der sonst auch alles syntaktisch falsche markiert meckert auch nicht.

Hat da jemand evt. eine Quelle ob das generell "richtig" ist, oder ob es von bestimmten Einstellungen von PHP abhängig ist (und Skripte dann nach einem Umzug nicht mehr funktionieren)?

Gruss,
Lenny

 
h3ll
20-04-2010, 10:37 
 
Hat da jemand evt. eine Quelle ob das generell "richtig" ist

Reicht dir das offizielle Handbuch (http://de3.php.net/manual/en/function.array.php) als Quelle nicht?

"Having a trailing comma after the last defined array entry, while unusual, is a valid syntax."

 
Lennynero
20-04-2010, 10:57 
 
Reicht dir das offizielle Handbuch (http://de3.php.net/manual/en/function.array.php) als Quelle nicht?

"Having a trailing comma after the last defined array entry, while unusual, is a valid syntax."

ups... genaues lesen hilft, bzw.... rtfm.

Danke für den Hinweis!


Alle Zeitangaben in WEZ +2. Es ist jetzt 21:53 Uhr.