Formatting associative array declaration

Posted by Drew Stephens on Stack Overflow See other posts from Stack Overflow or by Drew Stephens
Published on 2009-07-19T05:42:18Z Indexed on 2010/04/04 15:43 UTC
Read the original article Hit count: 235

When declaring an associative array, how do you handle the indentation of the elements of the array? I've seen a number of different styles (PHP syntax, since that's what I've been in lately). This is a pretty picky and trivial thing, so move along if you're interested in more serious pursuits.

1) Indent elements one more level:

$array = array(
    'Foo' => 'Bar',
    'Baz' => 'Qux'
    );

2) Indent elements two levels:

$array = array(
        'Foo' => 'Bar',
        'Baz' => 'Qux'
        );

3) Indent elements beyond the array constructor, with closing brace aligned with the start of the constructor:

$array = array(
            'Foo' => 'Bar',
            'Baz' => 'Qux'
        );

4) Indent elements beyond the array construct, with closing brace aligned with opening brace:

$array = array(
            'Foo' => 'Bar',
            'Baz' => 'Qux'
              );

Personally, I like #3—the broad indentation makes it clear that we're at a break point in the code (constructing the array), and having the closing brace floating a bit to the left of all of the array's data makes it clear that this declaration is done.

© Stack Overflow or respective owner

Related posts about php

Related posts about coding-style