4 ms·
I'm of the opinion you should be writing your PHP in a readable fashion similar to this: $foo = array( 'bar' => TRUE, 'baz' => 'socks' ) (pr
by dsl 15y ago
I'm of the opinion you should be writing your PHP in a readable fashion similar to this:
$foo = array(
'bar' => TRUE,
'baz' => 'socks'
)
(pretend you also have proper tabs as well)
- tsuraan 15y agoIt's been a long time since I did any PHP, but are trailing commas illegal? Normally, it seems like languages follow one of these two patterns, depending on whether their syntax allows extraneous commas or not: foo = [ 'first', 'second', 'third', ] or foo = [ 'first' , 'second' , 'third' ] The difference between those two examples and yours being that when you want to add a new entry, you only have to edit the line of the entry, rather than having to edit the line before the entry (to put in the comma) as well as the line that you care about. Just an idle question, really. EDIT: pull unnecessary commentary
- jasonlotito 15y agoNo, trailing commas are not illegal. It's actually preferred. $a = array(1,2,3,); works just fine.
- mcantelon 15y agoSure but this seems better: $foo = [ 'bar': TRUE, 'baz': 'socks' ]
- rorrr 15y agoIt's kind of JSON-wish, so this would make more sense: $bar = [1, 2, 3]; $foo = { 'bar': TRUE, 'baz': 'socks' };
- ars 15y agoWhat would be your syntax when you mix them? Which is perfectly legal to do: array(1, 'bar' => TRUE, 3, 'baz' => 'socks'); But I do like this syntax idea.
- nir 15y agoIIRC in PHP every array is internally a hash (ie "associative array"), so $bar is {0:1, 1:2, 2:3} in any case?
- z92 15y agoFor being JSONish it should be $foo = { bar: TRUE, baz: 'socks' }; where keys are unquoted. The less typing the better.
- swivelmaster 15y agoHowever, PHP allows me to do this: $keyName = "bar"; $array = array($keyName => "baz"); So in that instance it makes sense to force people to quote their keys. Also, bar or baz could be constants.
- rorrr 15y agoThat's against the spec. json_decode() will not even decode this. http://www.json.org/ http://www.json.org/ I know some browsers allow it, but it's not true JSON.
- Todd 15y agoThis is more perlish, with the exception of the map delimiter. If you proposed => it would be identical to perl.
- phaylon 15y agoAlmost, but Perl's => also auto-quotes barewords (and only barewords) on the left hand side, so foo => 23 is identical to "foo" => 23.