Those programmers who took computer science classes in C++ or Java and then started looking at popular open source PHP scripts may be surprised that the coding standards are somewhat different from what they are used to. Unfortunately, it’s hard to nail down good coding standards for PHP because it has so many syntaxes and does so many things. On one side, PHP is a serious object-oriented language and demanding of java-like syntax, but on another it’s a scripting language and very close to HTML.
One of the older and widely adopted PHP coding standards is based on Todd Hoff’s C++ Coding Standard and is available at http://www.dagbladet.no/development/phpcodingstandard/. Though it is widely used, and (as of the time of writing this) the number one result for “php coding standards” on Google, I give this the least cred because not only was it adopted from a C++ standard but it was not put together by an open source community or company.
The two “most official” coding standards are the PEAR PHP Standards (http://pear.php.net/manual/en/standards.php) and the Zend Framework Coding Standard for PHP (http://framework.zend.com/manual/en/coding-standard.html).
There are also other PHP Standards the are specific to certain projects, like Drupal (http://drupal.org/coding-standards) or Wordpress (http://codex.wordpress.org/WordPress_Coding_Standards), and are certainly valid as these frameworks have thousands of developers adding to their codebase, but they tend to be less detailed the above “official” coding standards.
I personally like the Zend standards the best, as they are the most detailed and easy to follow, but I actually prefer the variable and function naming convention of the Wordpress standard which uses all lowercase words separated by underscores (my_function()) as opposed to the more popular Java style function names (myFunction()) used by the other standards.
At some point in the future, I may write my own coding standards and publish them on this site, combining the best elements from each standard.
I would agree with the Zend standards standards. I think standards aid in future modifications and updates.
I seen some code that looked like someone poured it into a pot and boiled it then poured it out. Yikes!!
I will write more lines of code if I feel it helps the code to be self documenting.
Keep it simple stupid!!
Dear Mr. Elliott Brueggeman
What You have written is very true. I am disagree upon the fact “PHP is a serious object-oriented language and demanding of java-like syntax”
PHP is good but it never compete JAVA. moreover it is not a pure Object oriented Language. (Do we have method overloading?)
Anyway very inspirational article
Regards
Ankur Pandey
Once again, a Java developer needs to defend the ‘better’ language.
Everybody knows that:
- Java and PHP are 2 separate and different languages.
- not everything is possible in PHP and that it is more of a scripting language.
- calculations and jobs might be better in Java..
However, PHP is becoming, step by step, a ’sort of’ full programming language in my opinion and knowledge of Java is a superb addition to your PHP writing skills, not a disadvantage.
everybody likes to defend it’s own language, but a language is not better then an other one, it is made for another goal.
But, this is a discussion that thousands of developers already had…
good article, but an addition might be a list of good books on this topic:
- Code complete 2
- the pragmatic programmer
Those really are good books and explain it without pointing in 1 language as a standard is there for everyone not for PHP, Java, C, C++, Perl or any language in particular.
greetings,
and keep up your good work on the blog!!!