PHP is known as dirty but quick programming language. Although in other programming languages, user opinion, PHP program is not simple and beautiful, but we can pass some good programming habits, but also allow PHP code looks more comfortable and elegant. What are the methods? Below each of said next.
1. Planning Code Structure
2. Uniform Coding Style
Excellent PHP code should have a unified style. For example, variables, and functions to develop uniform naming convention for the cyclic tasks (such as database access, error handling) to develop uniform standards of access, or to maintain a regular code indentation, these coding practices can let others read the code more easy.
Excellent PHP code should be portable. PHP programmers should learn to use existing features (such as magic quotes and short labels, etc.), you should understand the product needs to adapt to the characteristics of PHP, PHP code written guarantee portability and cross-platform.
4. Security Code
Excellent PHP code should have security. PHP5 has excellent features and flexibility, but security applications are often in the hands of programmers. As a professional PHP developers should have some understanding of security vulnerabilities, common security vulnerabilities cross-site scripting attacks (XSS), cross-site request forgery (CSRF), code injection vulnerabilities and character encoding loopholes. Use PHP specific features and functions (such as mysql_real_escape_string, etc.) can help programmers write secure code.
5. Add comments
Code comments are an important part of the code that explains the purpose of the function is, and this will provide a very useful comments in the code to help future maintenance.
6. Avoid shorthand tag
Should use the full start flag is not recommended to use shorthand starting mark.
7. Use single quotes instead of double quotes
Because PHP will double quotes variable content search, in order to avoid the impact of this search brings performance, programmers should use single-quoted strings.
8. escape output
ENT_QUOTES parameters should be used in the htmlspecialchars function, to ensure that the single quotation mark ( ') can also be escaped. Although there is no requirement to do so, but this is a good habit.
9. Use a comma separated string output
String concatenation operator (.) Can be a single string to echo statements output, compared with a comma can echo statement outputs a string, which PHP is a performance improvement.
10. Check the value before the transfer output
It should be recalled pass values output before checking $ _GET [ 'query'] of. Use isset function or empty function can check whether the value of the variable is empty.