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 = []; / /… foreach (items=[]; / /… foreach(items as item) { // process on item … }

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']} echo with sprintf; 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); $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} We can also use isset to check if the value is not null. if (isset($items['search_key'])) { // process }Copy the code

I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, Redis, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc. Many knowledge points can be free to share with you