Recommended: "You may want to correct these 5 bad PHP coding habits! Video Tutorial"
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 ... } }
foreach
and the array function (array_*
) can Handle empty arrays.
$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 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; } // ... // 其他代码 // ... }
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 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'] }
Four: Echo and sprintf methods are 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 of it! Instead of combining echo
and sprintf
, we can simply use the printf
method.
$name = "John Doe"; printf('Bonjour %s', $name);
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 }
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 }
We can also use isset
to check if the value is not null
.
if (isset($items['search_key'])) { // process }
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!