12

What is allowed between these two:

$value = 'value';
$user = 'John';
$timestamp = 1480927909;
$day = date('Y-m-d', $timestamp);

or

$value     = 'value';
$user      = 'John';
$timestamp = 1480927909;
$day       = date('Y-m-d', $timestamp);
6
  • 1
    as far as I know it, it doesn't require it. the only thing that must be aligned vertically are the phpdoc tags, but that's outside the psr-2 specification
    – Federkun
    Dec 5, 2016 at 9:04
  • 5
    Imo, it drastically improves readability so I always do this kind of aligning.
    – Daniel W.
    Dec 5, 2016 at 10:07
  • 6
    @DanFromGermany - it only improves readability in cases where the names are roughly the same length. As soon as you need to do it with a dataset where one or two of the elements have names that are much longer than the others, then it can actually start decreasing readability. It can also screw with your diffs if you need to adjust the offset of the = sign at some point in the future. For these reasons, I like use this kind of aligning sometimes, but certainly not always.
    – Simba
    Dec 5, 2016 at 10:14
  • 4
    The readability of these is highly subjective. For me, the aligned version is much less readable than the one indented by a single space character. Not even regarding the similar principle for function arguments and parameters..
    – helvete
    Apr 13, 2018 at 11:34
  • 1
    Robert "Uncle Bob" Martin argues persuasively against this sort of horizontal alignment in Clean Code: "... you are tempted to read down the list of variable names without looking at [the values]." I find this to be true. I read the unaligned example left-to-right, top-to-bottom, but the aligned example top-to-bottom first, then left-to-right. It's like [$value, $user, $timestamp, $day] = ['value', 'John', 1480927909, date('Y-m-d', $timestamp)], which is hard to understand.
    – CJ Dennis
    Nov 29, 2018 at 23:29

1 Answer 1

14

PSR-2 does not have specific rules for this kind of inter-line alignment:

From the PSR-2 Conclusion:

There are many elements of style and practice intentionally omitted by this guide. These include but are not limited to:

  • Declaration of global variables and global constants
  • Declaration of functions
  • Operators and assignment
  • Inter-line alignment
  • Comments and documentation blocks
  • Class name prefixes and suffixes
  • Best practices

Future recommendations MAY revise and extend this guide to address those or other elements of style and practice.

For what it's worth, inter-line alignment was discussed for PSR-1 by the PHP-FIG Group, but was removed from the final version:

The way-back original long-form PSR-1 covered inter-line alignment, globals, ternaries, assignment, and lots of other things. Those ended up getting removed for various reasons; their epitaph is at the conclusion of PSR-2.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.