Skip to content

Instantly share code, notes, and snippets.

@omartdev
Created April 4, 2018 08:30
Show Gist options
  • Save omartdev/7c03d64e42133515d9f65ef44b1309b1 to your computer and use it in GitHub Desktop.
Save omartdev/7c03d64e42133515d9f65ef44b1309b1 to your computer and use it in GitHub Desktop.
PHP Coding Standards
PHP Coding Standards
----------------------
Single and Double Quotes
echo '<a href="/static/link" title="Yeah yeah!">Link name</a>'; // if all are static
echo "<a href='$link' title='$linktitle'>$linkname</a>"; if mix of variable and string
---------------------
give [tab] when write any variable
------------
Use elseif, not else if
----------
<input name="<?php echo esc_attr( $name ); ?>" /> // use "/" at end
---------
foreach ( $foo as $bar ) { // space between ( ) start middle end
----------------------------
$x = $foo['bar']; // correct
$x = $foo[ 'bar' ]; // incorrect
$x = $foo[0]; // correct
$x = $foo[ 0 ]; // incorrect
$x = $foo[ $bar ]; // correct
$x = $foo[$bar]; // incorrect
----------------------------
switch ( $foo ) {
case 'bar': // correct
case 'ba' : // incorrect
}
--------------
function sum( $a, $b ): float { // sum( )
return $a + $b;
}
-----------
Use lowercase letters in variable, action/filter, and function names (never camelCase). Separate words via underscores.
-------------------
In general, readability is more important than cleverness or brevity.
isset( $var ) || $var = some_function();
----------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment