Don't use newline braces.
<?php
// Good:
if ( $foo ) {
send($foo);
} else {
rejection();
}
// Bad:
if ( $foo )
{
send($foo);
}
else
{
rejection();
}
</code>
Always use pascal case.
<?php
// Good:
class MyAwesomeClass { ... }
// Bad:
class terrible_class { ... }
Use camelCase.
<?php
// Good:
function myFoo( $bar ) { ... }
// Bad:
function my_foo($bar) { ... }
// Good:
if ( $foo == 1 ) {
doAThing();
} elseif ( $foo == 2 ) {
// probably should have used a switch statement at this point. ;)
} else {
// Yeah this is starting to go badly.
}
Indent your case statements, and the code inside the case statements. Don't forget the break;
<?php
// Good:
switch ( $option ) {
case 'foo':
doFoo();
break;
case 'bar':
doBar();
break;
default:
// Some sort of default
break;
}
Use underscore + lowercase.
<?php
// Good:
$my_foo = 1;
// Bad:
$myFoo = 1;
Always have a space between opening and closeing parens. Also, best number of params is 0. Every number after that is worse. If I see a function with more than 3 params, pepare for a war.
<?php
// Good:
$baz = goFoo( $bar );
// Bad:
$baz = goFoo($bar);
Always use spaces. Only use two spaces. Do not use tabs. Tabs make code look different on different setups (see: servers). Make it consistent by using spaces.
PHP arrays are bastards. They are a mix of associative indexed and scalar indexed items. They can contain anything. You could describe the PHP array as a Hash + Array all jumbled together in to some sort of frankenstein data type.
Having two or three items is fine to keep inline, more than that put each on it's own line. Indent only two spaces.
<?php
// Good:
$my_foo = array( 'item1', 'item2', 'item3');
$my_foo = array(
'item1',
'item2',
'item3',
'item4',
'item5',
'item6'
);
// Bad:
$my_foo = array( 'item1', 'item2', 'item3', 'item4', 'item5', 'item6' );
Also, do nice indenting when you have arrays of arrays:
<?php
$my_foo = array(
'item1' => 1,
'other' => array(
'sub1' => 1,
'sub2' => 2
),
'item3' => 3
);
Group your code into similar functionality. I like to structure my functions like this: define local variable stuff, newline, process something, newline, return.
<?php
// Good:
class Multiplication {
// Need to set this somewhere.
private $multiplier = false;
function mutiplyNumber ( $multiplicand ) {
$multiplier = $this->multiplier ? $this->multiplier : 2; // Default to two.
$product = $multiplicand * $multiplier;
return $product;
}
}
// Bad:
class Multiplication {
// Need to set this somewhere.
private $multiplier = false;
function mutiplyNumber ( $multiplicand ) {
$multiplier = $this->multiplier ? $this->multiplier : 2; // Default to two.
$product = $multiplicand * $multiplier;
return $product;
}
}
Always declared as uppercase, separated with underscores. This is just a PHP standard.
<?php
define('MAX_VOLUME', 11);
class Foo {
const MAX_FOO_COUNT = 5;
}
If your file only contains PHP, do not put in a closing PHP statement.
<?php
// Good:
<?php
class Something { ... }
--- EOF ---
// Bad:
<?php
class something { ... }
?>
--- EOF ---