Skip to content

Instantly share code, notes, and snippets.

@stevedev
Last active December 27, 2015 04:48
Show Gist options
  • Save stevedev/7269026 to your computer and use it in GitHub Desktop.
Save stevedev/7269026 to your computer and use it in GitHub Desktop.

Braces

Don't use newline braces.

<?php

// Good:

if ( $foo ) { 
  send($foo);
} else {
  rejection();
}

// Bad:

if ( $foo ) 
{ 
  send($foo);
} 
else 
{
  rejection();
}
</code>

Class Naming

Always use pascal case.

<?php

// Good:
class MyAwesomeClass { ... }

// Bad:
class terrible_class { ... }

Function Naming

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. 
}

Switch Statements

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;
}

Variable naming

Use underscore + lowercase.

<?php

// Good:
$my_foo = 1;

// Bad:
$myFoo = 1;

Function Params

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);

Indenting:

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.

Array/Hash definitions

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
);

New lines

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;
  }
}

Constants:

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;
}

Opening/closing PHP Statements

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 ---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment