Skip to content
This repository has been archived by the owner on Jun 23, 2018. It is now read-only.

Latest commit

 

History

History
129 lines (95 loc) · 2.9 KB

php-style.md

File metadata and controls

129 lines (95 loc) · 2.9 KB
layout title permalink
library
PHP Style
/library/php-style/
  • Will be replaced with the ToC, excluding the "Contents" header {:toc}

Quotes

Use single and double quotes when appropriate. If you’re not evaluating anything in the string, use single quotes. You should almost never have to escape quotes in a string, because you can just alternate your quoting style, like so:

echo '<a href="/static/link" title="Yeah yeah!">Link name</a>';
echo "<a href='$link' title='$linktitle'>$linkname</a>";

Source: https://make.wordpress.org/core/handbook/coding-standards/php/#single-and-double-quotes

Indentation

Indentation should always reflect logical structure. Use real tabs and not spaces, as this allows the most flexibility across clients.

Brace Style

Braces should be used for all blocks in the style shown here:

if ( condition ) {
    action1();
    action2();
} elseif ( condition2 && condition3 ) {
    action3();
    action4();
} else {
    defaultaction();
}

Shorthand PHP Tags

Never use shorthand PHP start tags. Always use full PHP tags.

Right:

<?php ... ?>
<?php echo $var; ?>

Wrong:

<? ... ?>
<?= $var ?>

Spaces

Always put spaces after commas, and on both sides of logical, comparison, string and assignment operators.

x == 23
foo && bar
! foo
array( 1, 2, 3 )
$baz . '-5'
$term .= 'X'

Put spaces on both sides of the opening and closing parenthesis of if, elseif, foreach, for, and switch blocks.

foreach ( $foo as $bar ) { ...

When defining a function, do it like so:

function my_function( $param1 = 'foo', $param2 = 'bar' ) { ...

When calling a function, do it like so:

my_function( $param1, func_param( $param2 ) );

When performing logical comparisons, do it like so:

if ( ! $foo ) { ...

When referring to array items, only include a space around the index if it is a variable, for example:

$x = $foo['bar']; // correct
$x = $foo[ 'bar' ]; // incorrect
 
$x = $foo[0]; // correct
$x = $foo[ 0 ]; // incorrect
 
$x = $foo[ $bar ]; // correct
$x = $foo[$bar]; // incorrect

Source: https://make.wordpress.org/core/handbook/coding-standards/php/#space-usage

Naming Conventions

Use lowercase letters in variable, action, and function names (never camelCase). Separate words via underscores. Don’t abbreviate variable names un-necessarily; let the code be unambiguous and self-documenting.

function some_name( $some_variable ) { [...] }

Class names should use capitalized words separated by underscores. Any acronyms should be all upper case.

class Walker_Category extends Walker { [...] }
class WP_HTTP { [...] }

Constants should be in all upper-case with underscores separating words:

define( 'DOING_AJAX', true );

Files should be named descriptively using lowercase letters. Hyphens should separate words.

my-file-name.php