Reprinted from PHP forum: learnku.com/php/t/49583

After doing a lot of code reviews, I often see recurring errors, and here’s how to correct them.

Test if the array is empty before looping

$items = []; / /... if (count($items) > 0) { foreach ($items as $item) { // process on $item ... }}Copy the code

Foreach and the array function (array_*) can handle empty arrays.

  • You don’t need to test it first
  • Can reduce indentation by one layer
$items = [];
// ...
foreach ($items as $item) {
    // process on $item ...
}

Copy the code

Encapsulate the code content into an if statement summary

function foo(User $user) {
    if (!$user->isDisabled()) {
        // ...
        // long process
        // ...
    }
}

Copy the code

This isn’t a PHP-specific situation, but I see it all the time. You can reduce the indentation by returning earlier.

All major methods are at the first indentation level

function foo(User $user) { if ($user->isDisabled()) { return; } / /... // Other code //... }Copy the code

Call the isset method multiple times

You may encounter the following situations:

$a = null; $b = null; $c = null; / /... if (! isset($a) || ! isset($b) || ! isset($c)) { throw new Exception("undefined variable"); } / / or if (isset ($a) && isset ($b) && isset ($c) {/ / process with $a, $b, et $c} / / or $items = []; / /... if (isset($items['user']) && isset($items['user']['id']) { // process with $items['user']['id'] }Copy the code

We often need to check that a variable is defined. PHP provides an isset function to check for this variable, and this function can take more than one argument at a time, so it might be better to do this:

$a = null; $b = null; $c = null; / /... if (! isset($a, $b, $c)) { throw new Exception("undefined variable"); } / / or if (isset ($a, $b, $c)) {/ / process with $a, $b, et $c} / / or $items = []; / /... if (isset($items['user'], $items['user']['id'])) { // process with $items['user']['id'] }Copy the code

The echo and sprintf methods are used together

$name = "John Doe";
echo sprintf('Bonjour %s', $name);

Copy the code

You might want to laugh at this code, but I did write it for a while, and I still see it a lot! In fact, echo and Sprintf don’t need to be used at the same time, printf can fully print.

$name = "John Doe";
printf('Bonjour %s', $name);

Copy the code

Check for keys in the array by combining the two methods

$items = [
    'one_key' => 'John',
    'search_key' => 'Jane',
];

if (in_array('search_key', array_keys($items))) {
    // process
}

Copy the code

The last mistake I often see is the combination of in_array and array_keys. All of these can be replaced with array_KEY_EXISTS.

$items = [
    'one_key' => 'John',
    'search_key' => 'Jane',
];

if (array_key_exists('search_key', $items)) {
    // process
}

Copy the code

We can also use isset to check if the value is not null.

if (isset($items['search_key'])) {
    // process
}
Copy the code

For discussion, please visit the professional PHP forum: learnku.com/php/t/49583