These 5 PHP coding habits must not be committed during an interview! ! !

These questions must not be made during the interview! ! !

1. Test whether the array is empty before looping

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

foreach and array functions (array_*) can handle empty arrays.

No need to test first

Can reduce one level of indentation

$items = [];
// ...
foreach ($items as $item) {
    
    
    // process on $item ...
}

2. Encapsulate the code content into an if statement summary

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

This is not unique to PHP, but I often encounter such situations. You can reduce the indentation by returning early.

All main methods are at the first indentation level

function foo(User $user) {
    
    
    if ($user->isDisabled()) {
    
    
        return;
    }
 
    // ...
    // 其他代码
    // ...
}

Three, 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");
}
 
// 或者
 
if (isset($a) && isset($b) && isset($c) {
    
    
    // process with $a, $b et $c
}
 
// 或者
 
$items = [];
//...
if (isset($items['user']) && isset($items['user']['id']) {
    
    
    // process with $items['user']['id']
}

We often need to check whether a variable is defined. PHP provides the isset function to detect the variable, and the function can accept multiple parameters at once, so the following code may be better:

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

Four, echo and sprintf methods used together

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

This code may be smiling, but I happened to write it for a while. And I still see a lot! Instead of combining echo and sprintf, we can simply use the printf method.

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

Five, check whether there is a key in the array by combining the two methods

$items = [
    'one_key' => 'John',
    'search_key' => 'Jane',
];
 
if (in_array('search_key', array_keys($items))) {
    
    
    // process
}

The last mistake I often see is the combined use 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 whether the value is not null.

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

Pay attention, don't get lost

Alright, everyone, the above is the entire content of this article. The people who can see here are all talents . As I said before, there are a lot of technical points in PHP, because there are too many, it is really impossible to write, and you will not read too much after writing it, so I will organize it into PDF and documents here, if necessary Can

Click to enter the secret code: PHP+「Platform」

Insert picture description here

Insert picture description here


For more learning content, please visit the [Comparative Standard Factory] excellent PHP architect tutorial catalog, as long as you can read it to ensure that the salary will rise a step (continuous update)

The above content hopes to help everyone . Many PHPers always encounter some problems and bottlenecks when they are advanced. There is no sense of direction when writing too much business code. I don’t know where to start to improve. I have compiled some information about this, including But not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripts, Docker, microservices, Nginx, etc. Many knowledge points, advanced advanced dry goods, can be shared with everyone for free, and those who need can join my PHP technology exchange group

Guess you like

Origin blog.csdn.net/weixin_49163826/article/details/108890670