For starters, it may be difficult to understand why some practices are safer.
However, some of the following tips may be beyond the scope of PHP.
Always use braces
Let's look at the following code:
if (isset($condition) && true === $condition) echo 'this is a success'; //It's true, but look at the code below if (isset($condition) && true === $condition) foreach (range("A", "Z") as $letter) echo $letter . PHP_EOL; echo 'this is a success';
It's still true, but this is a success only echo once in the end, which may be a trap.
Wait a minute. Let's see what happens before we write $condition = false
$condition = false; if (isset($condition) && true === $condition) foreach (range("A", "Z") as $letter) echo $letter . PHP_EOL; echo 'this is a success';
This is obviously not the result we want. Do you know why curly braces are important now?
Omitting parentheses usually leads to undesirable side effects. In addition, reading code is much more difficult. So it should be rewritten like this
$condition = false; if (isset($condition) && true === $condition) { foreach (range("A", "Z") as $letter) { echo $letter . PHP_EOL; } echo 'this is a success'; }
Skip else section
It is best to initialize any variable before using it. Consider the following example:
if (isset($condition) && true === $condition) { $main = 'chocolate'; } else { $main = 'vanilla'; }
If you think the above example is OK, try adding a few else if conditions
if ($condition) { $main = 'chocolate'; $time = 2; } elseif($otherCondition)) { $main = 'strawberry'; $time = 3; } elseif($otherOtherCondition)) { $main = 'apple'; $time = 1; } else { $main = 'vanilla'; $time = 9; }
The code is cumbersome. You may forget some variables when you add logic to an else if block of code.
Try this:
$main = 'vanilla'; $time = 9; if ($condition) { $main = 'chocolate'; $time = 2; } elseif($otherCondition)) { $main = 'strawberry'; $time = 3; } elseif($otherOtherCondition)) { $main = 'apple'; $time = 1; }
In this way, everyone knows that the $main and $time variables should be defined in the following code, which seems to be more readable.
In this way, everyone knows that the $main and $time variables should be defined in the following code, which seems to be more readable.
Do not use the @ operator
In PHP @ is an error control operator. It's used to hide mistakes, but no one really wants to.
Instead, you should try to capture these errors and use error logging.
@Operators have great disadvantages, such as turning any debugging into a nightmare or making code run slower!
For each run of the code, it calls the INI setting to set error reporting to 0, and then sets it back to the original value.
It's so resource intensive. I think it's safer not to use it at all.
Some ideas about ternary operators
While ternary operators can be handy, the code can be more difficult to read.
Do not overuse ternary operators
The ternary operation can be used to simplify the if else part:
$main = ($condition) ? "chocolate" : "vanilla";
The meaning is as follows:
if ($condition) { $main = "chocolate"; } else { $main = "vanilla"; }
The above example is ok, but I have seen the following examples:
$output = ($condition) ? ($otherCondition) ? 'ok' : 'ko' : 'unknown';
It is not recommended to use ternary operations in a chained or nested way.
Use "Elvis" operator carefully
Elvis operator Is a special operator.
$foo = $bar ?: "baz";
The above refers to:
$foo = $bar ? $bar : "baz";
You can write as follows:
if ($bar) { $foo = $bar; } else { $foo = "baz"; }
Believe it or not, as Wikipedia says:
Be careful with empty merge operators
Since PHP7, null merge operator Available:
So:
$result = isset($_GET['result']) ? $_GET['result'] : "default";
You can write as follows:
$result = $_GET['result'] ?? "default";
Be careful when trying to replace the ternaries and Elvis operators with empty merge operators!
The empty merge operator never checks whether the condition is true, but whether your var is set instead of empty (the concept of isset()).
Sum up
I hope you like these programming skills. The last thing I want to say is: please don't sacrifice clarity for simplicity.