Skip to content
Advertisement

PHP PSR Whitespaces before and after concatenate operator question (dot)

I am looking for information about best practices when programming at PHP. I could not find any information about the space before and after the concatenate operator “dot” (.).

$var = "Hello " . $name . " How you doing? today: " . $day;

Maybe there is another standard that specifies this but I didn’t found it.

https://www.php-fig.org/psr/psr-2/

Advertisement

Answer

PSR-2 doesn’t cover a coding style for operators. They are listed in the conclusion as “intentionally omitted“.

PSR-12: Extended Coding Style expands on PSR-2, and does include guidance around this:

6.2. Binary operators All binary arithmetic, comparison, assignment, bitwise, logical, string, and type operators MUST be preceded and followed by at least one space:

if ($a === $b) {
    $foo = $bar ?? $a ?? $b;
} elseif ($a > $b) {
    $foo = $a + $b * $c;
}

See https://www.php-fig.org/psr/psr-12/#62-binary-operators

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement