How to make PHP code look better and read better

  • 2021-12-11 07:12:56
  • OfStack

Writing excellent program code is an art. If you want to do this, you must develop good programming habits at the beginning of 1. Good programming habits not only help the initial design of the project (such as modularization), but also make the code you write easier to understand, thus making the maintenance of the code easier and less labor-intensive. Bad programming habits will result in code bug, and will make later maintenance difficult.

This article takes PHP language as an example to introduce some good programming habits, hoping to be helpful to you.

1. Plan the code structure

Good PHP code should have a clear structure. The object-oriented nature of PHP allows programmers to break down an application into functions or methods. If the code is obscure, you can also add comments to make the function of the code clear. When coding, try to separate the front-end code (HTML/CSS/JavaScript) from the server-side rules of the application, or you can use the PHP framework that follows the MVC pattern to build your application.

2. Coding style system 1

Excellent PHP code should have the style of unification 1. For example, making Uniform 1 naming rules for variables and functions, making Uniform 1 access standards for circular tasks (such as database access and error handling), or keeping regular code indentation can make it easier for others to read code.

3. Portability

Good PHP code should be portable. Programmers should learn to use the existing features of PHP (such as magic quotation marks and short labels, etc.), understand the product requirements, adapt to the characteristics of PHP, and ensure that the written PHP code has portability and cross-platform.

4. Code security

Good PHP code should be secure. PHP 5 has excellent features and flexibility, but application security is often in the hands of programmers. As a professional PHP developer, you should have a deep understanding of security vulnerabilities. Common security vulnerabilities include cross-site scripting attack (XSS), cross-site request forgery (CSRF), code injection vulnerability and character encoding vulnerability. Using specific features and functions in PHP, such as mysql_real_escape_string, can help programmers write secure code.

Step 5 Add comments

Code comments are an important part of the code that explains the purpose of the function, and can be very useful for later maintenance of the code.

6. Avoid shorthand marks

Complete start tags should be used, and abbreviated start tags are not recommended.

7. Use single quotation marks instead of double quotation marks

Because PHP searches for variables in double quotation marks, to avoid the performance impact of this search, programmers should use single quotation marks to reference strings.

8. Escape output

The ENT_QUOTES parameter should be used in the htmlspecialchars function to ensure that single quotation marks (') can also be escaped. Although there is no requirement to do so, it is a good habit.

9. Use commas to separate string output

In contrast to the string concatenator (.), which can pass a single 1 string to the echo statement for output, commas can achieve separate output of strings in the echo statement, which is a performance improvement for PHP.

10. Check the passed value before output

Remember to check the passed value of $_ GET ['query'] before output. Use the isset function or the empty function to check whether the variable value is empty.


Related articles: