Home > News content

PHP code specification 10 good habits

via:博客园     time:2015/12/4 16:00:27     readed:1273

PHP is known as the but quick dirty programming language. Although in other programming language users, the PHP program is not simple and beautiful, but we can through some of the good programming habits, but also to make PHP code looks more comfortable and elegant. What are the methods? Below one by one.

1 programming code structure

Good PHP code should have a clear structure. PHP object-oriented features allow programmers to break down into a function or method. If the code is difficult to understand, you can also add comments, so that the function of code at a glance. Encoding should try to keep the front end code (HTML/CSS/JavaScript) separate from the application server, or you can use the PHP framework to build your application in MVC.

2 encoding style uniform

Good PHP code should have a unified style. For example, to develop a unified set of rules for variables and functions, for the circular tasks (such as database access, error handling) to develop a unified access standards, or to maintain a regular code indentation, these encoding habits can make people read more easily.

3 portability

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

4 code security

Good PHP code should be secure. PHP5 has excellent performance and flexibility, but the application of security is often in the hands of the programmer. As a professional developer, should be of security vulnerabilities some insight and common security vulnerabilities in a cross site scripting (XSS), Cross Site Request Forgery (CSRF), code injection vulnerabilities and the character encoding loopholes and so on. Using PHP specific functions and functions (such as mysql_real_escape_string, etc.) can help programmers write code of safety.

5 add comments

Code comment is an important part of the code, which explains the purpose of the operation of the function, which provides useful help in the maintenance of the code.

6 avoid - mark

The initial marking shall be complete, do not use the recommended starting mark -.

7 single quotes instead of double quotes

Since the PHP will search the contents of the double quotes, to avoid the performance of the search, the programmer should use a single quote to quote the string.

8 escape output

You should use the ENT_QUOTES parameter in the htmlspecialchars function, ensure the single quotes (') can also be escaped. It is a good habit to do so, though it is not a rule.

9 use comma separated string output

A string is a string that can be passed to the echo statement for a single string, compared with that of the echo statement, which is a performance improvement for the PHP.

10 pass value before output

It should be remembered that the pass value of the $_GET['query'] is checked before output. Using the isset function or empty function can check whether the variable value is empty.

China IT News APP

Download China IT News APP

Please rate this news

The average score will be displayed after you score.

Post comment

Do not see clearly? Click for a new code.

User comments