PHP is called the dirtybutquick programming language. Despite the fact that PHP programs are not concise and elegant to other programming language users, we can use some excellent programming habits to make PHP code look more comfortable and elegant. What are the methods? Let's talk about it one by one. 1. PHP code with a well-planned code structure should have a clear structure. PHP object-oriented features allow programmers to break down applications
PHP is called the dirty but quick programming language. Despite the fact that PHP programs are not concise and elegant to other programming language users, we can use some excellent programming habits to make PHP code look more comfortable and elegant. What are the methods? Let's talk about it one by one.
1. plan the code structure
Good PHP code should have a clear structure. PHP object-oriented features allow programmers to break down applications into functions or methods. If the code is obscure, you can add comments to make the code functions clear at a glance. When coding, try to separate the front-end code (HTML/CSS/JavaScript) from the application's server rules, or you can use the PHP framework following the MVC pattern to build your application.
2. unified encoding style
Excellent PHP code should have a uniform style. For example, develop unified naming rules for variables and functions, develop unified access standards for cyclic tasks (such as database access and error handling), or maintain regular code indentation, these coding habits make it easier for others to read the code.
3. portability
Excellent PHP code should be portable. Programmers should learn to use existing PHP features (such as magic quotes and short tags), understand product requirements, adapt to PHP features, and ensure that written PHP code is portable and cross-platform.
4. code security
Excellent PHP code should be secure. PHP5 has excellent features and flexibility, but the security of applications 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 (XSS) and cross-site request forgery (CSRF), code injection and character encoding vulnerabilities. Using specific functions and functions in PHP (such as mysql_real_escape_string) can help programmers write secure code.
5. Add comments
Code comments are an important part of the code. they explain the purpose of function execution. such comments can be very helpful for future code maintenance.
6. avoid shorthand mark
The full start mark should be used. it is not recommended to use the short start mark.
7. replace double quotation marks with single quotes
Because PHP searches for the content in double quotation marks for variables, to avoid the performance impact of such searches, programmers should use single quotes 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 rule to do this, it is a good habit.
9. use commas to separate string output
String connector (.) A single string can be passed to the echo statement for output. In contrast, a comma can implement separate output of strings in the echo statement, which is a performance improvement for PHP.
10. check transfer value before output
Check the transfer value of $ _ GET ['query'] before output. Use the isset function or empty function to check whether the variable value is null.