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
Solution:
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.
Hope it helps!
Answer
Solution:
+1 for spaces :)
but that's just my own standard, as long as you are consistent and your code is clear, it should be okay
Answer
Solution:
PHP is much like C in its syntax. As I use both, I tend to follow the same style.
For instance, keywords vs functions:
vs
Then you come to a question of indentation:
.. is much easier to read as
This indicates that the switch and the case are on the same level, which they are. It also helps avoid crazy indentation in yet-to-be-optimal switches.
Your style should illustrate the syntactic sugar of the language you are using. This gets strange with PHP as well as C, because both have forgiving parsers.
If you write something like this:
I'm going to hunt you down and demand that you pay for my aspirin. The same would go for this:
... For God sakes man, its not LISP!
Answer
Solution:
It's a matter of convetions that are stablished within your team.
The most famous conventions are Zend Framework and PEAR. You can also create your own, just make sure it is readible.
Personally, I use spaces.
Answer
Solution:
The general advice would be to standardize the code formating, so it meets some best practices, and is widely known, instead of inventing something custom.
, the coding style guide would be the best well known approach, accepted by many. here are , why it's good to keep the code formatting.