Darryl Hein Darryl Hein - 8 months ago 37
PHP Question

Should PHP code have spaces before operators?

I've seen a lot of code recently formatted as the following:


if ($var=='test'){
// do something

Personally I don't like it and would prefer it as:


if ($var == 'test') {
$var = Foo('blah' . $var1);
} else {
// do something

I think it's much more readable (note the addition of spaces).

Is there a general preference in the community or is one way better than another.


The most important thing is to follow a standard and stick to it.

That said maybe you can follow Zend's Framework standards and they use spaces. Check C.4.6.

if ($a != 2) {
    $a = 2;

Hope it helps!