Best practices

There are other things not covered by PEAR Coding Standards which are mostly subject of personal preference and not directly related to readability of the code. Things like "single quotes vs double quotes" are features of PHP itself to make programming easier and there are no reasons not use one way in preference to another. Such best practices are left solely on developer to decide. The only recommendation could be made to keep consistency within package and respect personal style of other developers.

Readability of code blocks

Related lines of code should be grouped into blocks, separated from each other to keep readability as high as possible. The definition of "related" depends on the code :)

For example:

<?php

if ($foo) {
    
$bar 1;
}
if (
$spam) {
    
$ham 1;
}
if (
$pinky) {
    
$brain 1;
}
?>

is a lot easier to read when separated:

<?php

if ($foo) {
    
$bar 1;
}

if (
$spam) {
    
$ham 1;
}

if (
$pinky) {
    
$brain 1;
}
?>

Return early

To keep readability in functions and methods, it is wise to return early if simple conditions apply that can be checked at the beginning of a method:

<?php

function foo($bar$baz)
{
    if (
$foo) {
        
//assume
        //that
        //here
        //is
        //the
        //whole
        //logic
        //of
        //this
        //method
        
return $calculated_value;
    } else {
        return 
null;
    }
}
?>

It's better to return early, keeping indentation and brain power needed to follow the code low.

<?php

function foo($bar$baz)
{
    if (!
$foo) {
        return 
null;
    }

    
//assume
    //that
    //here
    //is
    //the
    //whole
    //logic
    //of
    //this
    //method
    
return $calculated_value;
}
?>
Error Handling Guidelines (Previous) Sample File (including Docblock Comment standards) (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: lynch
No, the interpolation of double quotes is NOT significant.

This is because single quotes must also read the whole string, char by char, looking for:
\'
\\

The double-quote version merely has a larger number of 'case' statements to apply for $var and embedded hex values.

The expensive part, the iteration, is the same for both.
Note by: bobvandell@hotmail.com
Try this site to check the speed of single quotes versus double quotes:

http://www.phpbench.com

I've found that it has a lot of best practices to help you out as well.
One major one for me was counting the length of a loop before hand instead of doing it inline:

$len = count($arr);

for ($i = 0; $i < $len; $i++)
{
// Code
}

Is faster than
for ($i = 0; $i < count($arr); $i++)
{
// Code
}
Note by: natecmichael@gmail.com
Micro-optimization like "single quotes are faster" is unnecessary!
Note by: Maga
Interpolation makes script slower, it matters! Single quotes are faster!
Note by: Leonardo Herrera
Double quotes interpolate, single quotes don't. Isn't this important?