Skip to content
This repository has been archived by the owner on Sep 22, 2021. It is now read-only.

Latest commit

 

History

History
89 lines (65 loc) · 1.39 KB

php.md

File metadata and controls

89 lines (65 loc) · 1.39 KB

Best Practices: PHP

Brackets

When writing functions, place the leading bracket on the same line of the function declaration, and the trailing bracket on its own line:

INCORRECT:

function foo()
{
  // Do the things
}

CORRECT:

function foo() {
  // Do the things
}

Quotes

Use single quotes to denote strings unless the string contains characters that would need to be escaped.

Example

INCORRECT:

$foo = "bar";

CORRECT:

$foo = 'bar';

INCORRECT:

$foo = 'I don\'t like that it\'s raining';

CORRECT:

$foo = "I don't like that it's raining";

Spacing

Keep your PHP code readable to humans (i.e. the rest of us). The great thing about PHP is that you can put space between blocks and chunk related lines together to make it easier to decipher.

  • Add a space after function definition and the leading bracket
  • Add a new line before a function's return call
  • Pad function parameters in a function definition
  • If passing a variable to a function, pad it with space. If sending only a string, no space is necessary

Examples

INCORRECT:

function foo($dogs){
  $bar=$dogs;
  return $bar;
}
foo('cats');

CORRECT:

function foo( $dogs ) {
  $bar = $dogs;
  
  return $bar;
}

// Incorrect
$foo = 'cats';
foo($foo);

// Correct
foo('cats');

// Correct
$foo = 'cats';
foo( $foo );