You may want to correct these 5 bad PHP coding habits!

藏色散人
Release: 2023-04-09 15:22:02
forward
4408 people have browsed it

Recommended: "You may want to correct these 5 bad PHP coding habits! Video Tutorial"

You may want to correct these 5 bad PHP coding habits!

After doing a lot of code reviews, I often see some duplicates Errors, here's how to correct them.

1: Test whether the array is empty before looping

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

foreach and the array function (array_*) can Handle empty arrays.

  • No need to test first
  • Can reduce one level of indentation
$items = [];
// ...
foreach ($items as $item) {
    // process on $item ...
}
Copy after login

2: Encapsulate the code content into an if statement summary

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

This is not a You may want to correct these 5 bad PHP coding habits!-specific situation, but I often encounter such situations. You can reduce indentation by returning early.

All main methods are at the first indentation level

function foo(User $user) {
    if ($user->isDisabled()) {
        return;
    }

    // ...
    // 其他代码
    // ...
}
Copy after login

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']
}
Copy after login

We often need to check whether a variable has been defined. You may want to correct these 5 bad PHP coding habits! provides the isset function that can be used to detect the variable, and the function can accept multiple parameters at one time, 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']
}
Copy after login

Four: Echo and sprintf methods are used together

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

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

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

Five: Check if a key exists in an array by combining two methods

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

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

The last mistake I often see is in_array and Joint use of array_keys. All of these can be replaced using array_key_exists.

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

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

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

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

Original address: https://dev.to/klnjmm/5-bad-habits-to-lose-in-php-2j98

Translation address: https:/ /learnku.com/php/t/49583

The above is the detailed content of You may want to correct these 5 bad PHP coding habits!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!