Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: PHP Coding Guidelines  (Read 2915 times)

centered

  • Guest
PHP Coding Guidelines
« on: February 02, 2009, 03:37:24 am »

Not sure if this is the right forum to post this, but...

A few times I have noted publicly and privately about coding standards.  A few times some have debated against my coding practices because I try to follow some of these standards. In addition, as we move to a plugin based system, I personally would like to see everyone coding in a similar fashion as the base CMS. So, let me outline some of these standards I have collected for PHP:

http://www.evolt.org/article/PHP_coding_guidelines/18/60247/
Quote from: Evolt Excerpt
Spaces between tokens

There should always be one space on either side of a token in expressions, statements etc. The only exceptions are commas (which should have one space after, but none before), semi-colons (which should not have spaces on either side if they are at the end of a line, and one space after otherwise). Functions should follow the rules laid out already, i.e. no spaces between the function name and the opening bracket and no space between the brackets and the arguments, but one space between each argument.

Control statements such as if, for, while etc. should have one space on either side of the opening bracket, and one space before the closing bracket. However, individual conditions inside these brackets (e.g. ($i < 9) || ($i > 16)) should not have spaces between their conditions and their opening/closing brackets.

In these examples, each pair shows the incorrect way followed by the correct way:

Code: [Select]
<?php
$i
=0;
$i 0;

if(( 
$i<)||( $i>))
if ( (
$i 2) || ($i 5) )

foo $a,$b,$c )
foo($a$b$c)

$i=($j<5)?$j:5
$i 
= ($j 5) ? $j 5
?>

http://cvs.php.net/viewvc.cgi/php-src/CODING_STANDARDS?revision=1.32.4.3.2.1.2.3&view=co

http://wordpress.org/docs/developer/coding-style/

http://pcspectra.wordpress.com/2008/12/20/78/ - “Don’t write code that just works, write code that someone else can understand.”

http://www.sitepoint.com/article/coding-standards/
Quote from: Sitepoint Excerpt
SQL code layout: Since we'll all be using different editor settings, don't try to do anything complex like aligning columns in SQL code. Do, however, break statements onto their own lines. Here's a sample of how SQL code should look. Note where the lines break, the capitalisation, and the use of brackets, examples:

Code: [Select]
SELECT field1 AS something, field2, field3
     FROM table a, table b
     WHERE (this = that)
         AND (this2 = that2)

Inline conditionals: Inline conditionals should only be used to do very simple things. Preferably, they will only be used to do assignments, and not for function calls or anything complex at all. They can be harmful to readability if used incorrectly, so don't fall in love with saving typing by using them, examples:

Code: [Select]
Bad place to use them:
(($i < $size) && ($j > $size)) ? do_stuff($foo) : do_stuff($bar);

Ok place to use them:
$min = ($i < $j) ? $i : $j;

« Last Edit: February 03, 2009, 03:29:21 am by equilni »
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6020
  • Semantically Challenged
    • snews.ca
Re: PHP Coding Guidelines
« Reply #1 on: February 02, 2009, 04:01:42 pm »

You have been busy EQ. Perhaps it would be sufficient to simply post the links to all of these articles (from other websites) in one post, rather than post the link and the full article separately as you have. This would reduce the length of this thread down to a few inches ( screen) and make it much more useful as a single resource reference entry.
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

centered

  • Guest
Re: PHP Coding Guidelines
« Reply #2 on: February 04, 2009, 04:28:03 am »

Done.
Logged