PHP is called a dirty but quick programming language. Although in the eyes of users of other programming languages, PHP programs are not simple and beautiful, we can make PHP code look more comfortable and elegant through some excellent programming habits. What are the methods? Let’s talk about them one by one.
Excellent PHP code should have a clear structure. The object-oriented nature of PHP allows programmers to break down applications into functions or methods. If the code is obscure, you can also add comments to make it clear what the code does. When coding, try to separate the front-end code (HTML/CSS/JavaScript) from the server-side rules of the application, or you can use a PHP framework that follows the MVC pattern to build your application.
Excellent PHP code should have a unified style. For example, develop unified naming rules for variables and functions, develop unified access standards for cyclic tasks (such as database access, error handling), or maintain regular code indentation. These coding habits can make others read the code more easily. easy.
Good PHP code should be portable. Programmers should learn to use PHP's existing features (such as magic quotes and short tags, etc.), understand product requirements, adapt to PHP's characteristics, and ensure that the written PHP code is portable and cross-platform.
Good PHP code should be secure. PHP5 has excellent features and flexibility, but the security of the application is often in the hands of the programmer. As a professional PHP developer, you should have some in-depth understanding of security vulnerabilities. Common security vulnerabilities include cross-site scripting attacks (XSS), cross-site request forgery (CSRF), code injection vulnerabilities, and character encoding vulnerabilities. Using specific features and functions in PHP (such as mysql_real_escape_string, etc.) can help programmers write safe code.
Code comments are an important part of the code. They explain the purpose of the function operation. Such comments will provide very useful help in future maintenance of the code.
The full start tag should be used, abbreviated start tags are not recommended.
Since PHP will perform variable search for content in double quotes, in order to avoid the performance impact of this search, programmers should use single quotes to quote strings.
The ENT_QUOTES parameter should be used in the htmlspecialchars function to ensure that single quotes (') can also be escaped. Although there is no requirement to do this, it is a good practice.
The string concatenator (.) can pass a single string to the echo statement for output. In contrast, the comma can realize the separate output of strings in the echo statement, which is a performance improvement for PHP.
You should remember to check the passed value of $_GET['query'] before outputting. Use the isset function or empty function to check whether the variable value is empty.