Darryl Hein Darryl Hein - 7 months ago 23
PHP Question

Should PHP code have spaces before operators?

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

A:



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


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

B:



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.

Answer

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!