PHP - acnorrisuk/coding-style-guide GitHub Wiki
Single quotes should be used for strings unless you're making use of double quotes to parse a string with variables in it.
You are free to use PHPs alternative control structure (i.e. endif
, endwhile
) syntax if it helps readability.
Similarly, you can choose to output HTML in whichever way affords the most readability.
<!-- Using single PHP quotes -->
<?php echo '<a href="' . $link . '" class="' . $class . '">Link</a>'?>
<!-- Using double PHP quotes (although this does lead to single quotes in HTML attributes) -->
<?php echo "<a href='$link' class='$class'>Link</a>"?>
<!-- Using printf() -->
<?php printf("<a href='%s' class='%s'>Link</a>", $link, $class);?>
<!-- Using PHP tags -->
<a href="<?php echo $link;?>" class="<?php echo $class;?>">Link</a>
Never use shorthand php tags as this could break compatibility with other languages (e.g. XML)
<!-- Avoid -->
<? ?>
<?= ?>
<!-- Better -->
<?php ?>
Omit the final PHP tag if it closes the document as this helps prevent errors involving extra whitespace.
Capitalise SQL queries to help readability
Use snake_case for naming variables
Lean towards readability in logic rather than cleverness
// A little confusing
isset( $var ) || $var = some_function();
// Clearer
if ( ! isset( $var ) ) {
$var = some_function();
}