code readability

Which method is easier to read?

<?php
$str = "Hello";
$number = 123;
 
printf("%s world. Day number %u",$str,$number); // method 1
echo $str . " world. Day number " . $number; // method 2
?>

Code readability is more important than micro-optimization because in most part of cases micro-optimization does not worth it.

In general, readability is more important than cleverness or brevity.

<?php
isset( $var ) || $var = some_function();
?>

Although the above line is clever, it takes a while to grok if you're not familiar with it. So, just write it like this:

<?php
if ( ! isset( $var ) ) {
    $var = some_function();
}
?>
<?php
if ($a OR $b) {} // 'OR' better than '||'
if ($a AND $b) {} // 'AND' better than '&&'

Leave a Reply

Your email address will not be published. Required fields are marked *