[PSR-12] Arithmetic operators without surrounding whitespace

39 views
Skip to first unread message

Magnar Ovedal Myrtveit

unread,
Aug 3, 2018, 5:11:08 AM8/3/18
to PHP Framework Interoperability Group
PSR-12 states that

All binary and ternary (but not unary) operators MUST be preceded and followed by at least one space

Sometimes I find it useful to write arithmetic operators without surrounding whitespace, since it can help make the meaning clear at a glance.

Consider this very simple example:
$foo = 1 + 2*3 + 4 - 5/7; // Syntax indicates the order in which the operations are performed.
$bar = 1 + 2 * 3 + 4 - 5 / 7; // Syntax does not indicate anything.

What are the reasons for requiring surrounding whitespace for (arithmetic) operators?

Magnar :)

Ian Littman

unread,
Aug 3, 2018, 5:14:29 AM8/3/18
to php...@googlegroups.com
The scenario you describe would be better served by parentheses, methinks. No, they aren't strictly necessary, but forcing the reader to think back to PEMDAS while reading through code rather than just looking at the parentheses...well...same sort of idea as requiring braces around single-ststement control flow blocks.

--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/5ba39cf5-f2ee-4581-9cfb-bc6acec0e19e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages