Check out some of the code that has not been very good (Part 1)

Tram Ho

The following article will point out a few possible scenarios when writing PHP code in general, Laravel in particular. These may be basic logical errors, improper function usage; Or simply convention errors are not worth it.

1. Using try-catch with incorrect catch order

Because Throwable is the parent class of Exception and ValidationException, in the above example, the exception to be thrown will be caught at the first catch and the two catch below will become meaningless.

When using try catch, pay attention to the order and paternity of the exception type.

The code above can be amended as follows:

2. Handling the same exception in the catch statement

In both cases catch , have the same treatment as call to report() . Then splitting into two catch statements becomes unnecessary.

If you only want to catch ValidationException, you should remove the catch statement below.

If you want to catch all exceptions and handle them in the same catch , you should remove the catch statement above.

3. Use unnecessary if syntax

Or maybe a little shorter.

The above two pieces of code are written incorrectly in logic but are quite redundant and lengthy. For simple comparative logic, consider shortening the following:

4. The variable declaration is only used once

Declaring the local variable $a in this case is not necessary.

For operations that are not too long and complex, the following should be considered:

5. Use the array_splice() function in the loop of the current array itself.

The array_splice() function is one of the solutions thought of in the problem of removing unwanted elements from an array.

In the above example, the array_splice() function is used to remove odd numbers inside the $numbers array.

The expected results when running the above code are:

However, in reality, the result returned is

The reason is that the array_splice() function changed the current array and caused an error when using the $key index variable. The value of $key no longer reflects the position of the element to be removed.

Can be verified by displaying data after each foreach loop

This is a fairly basic error but is sometimes overlooked, resulting in unforeseen results.

With the above example problem, there are many different solutions. Two of many ways to consider:

Use the for loop and reduce the value of the pointer variable at the time of “splice”

Using the array_filter() function

6. Assign values ​​containing space characters to Laravel’s environment variables

The variables in the dotenv file (of Laravel) do not support values ​​with an escaped whitespace character. Therefore, in the above example, the error will be dumped with the following content (Laravel 5.8):

Therefore, always remember to escape the values ​​of environment variables in Laravel.

7. Pass null values ​​into the function using type-hint

The above code generates an error (PHP Fatal error) when there is a difference in the data type between the argument and function arguments.

The above problem can be solved by either of the following two options:

Use nullable type (From PHP 7.1 and above)

Assigning default values ​​by null to function arguments

8. Use loose comparisons ( == ,! != ) On the strpos() function

The strpos() function is often used to determine whether a substring is in a given string or not by the position of the substring. If substring is not found, strpos() returns false .

For the above example, the function doesContain() will return true if string $str contains the character string $char .

However, when testing the letter H or the Hello character string, the result is false

The reason is that the first character / string (substring) in the first position will have an offset value of 0. Therefore, when comparing != With false values, the result will be false .

Therefore, strict comparisons ( === ,! !=== ) should be used when using the strpos() function strpos()

9. Optional parameters are preceded by mandatory parameters

The method declaration of the above function is valid but not valid.

When we want to call the foo() function with the value of the parameter $a equal to null , we will have to pass the value to both parameters. Although the first parameter has default values.


Therefore, always sort the last optional parameters after the function definition.
Share the news now

Source : Viblo