Precautions for learning PHP

1 Read the manual and source code more

There is nothing more worth emphasizing than reading the manual-you can learn a lot by just reading the manual, especially many functions related to strings and arrays. These functions include many useful functions. If you read the manual carefully, you will often find that in the past project development process, many times you are "reinventing the wheel", but in fact you only need a core function. Complete the corresponding function. The manual is your friend. In addition, there are many open source programs developed using PHP. Why not learn and borrow? Download a copy of the source code of an open source PHP application and read it carefully. Perhaps the bigger the items, the more worthwhile to read, although they may have more complex structures and systems, but they also have more detailed explanation documents.

2 Write modular code

Good PHP code should be modular code. PHP's object-oriented programming features are some particularly powerful tools that can decompose your application into functions or methods. You should separate the front-end HTML/CSS/JavaScript code from the server side of your application as much as possible. You can also follow the MVC (Model-View-Controller) pattern on any PHP framework.

3 Code writing specifications

Good PHP code should have a complete set of coding standards. Through the naming of variables and functions, a unified method to access the database and error handling, and the same code indentation method to achieve programming standards, which can make your code more readable.

4 Write portable code

Good PHP code should be portable. You can use the existing features of php, such as magic quotes and short tags. Try to understand your needs, and then write code to make the code independent and portable by adapting to the characteristics of PHP.

5 Write secure code

Good PHP code should be safe. PHP5 provides excellent performance and flexibility. But the security problem lies entirely with the developers. For a professional PHP developer, it is essential to have a deep understanding of major security vulnerabilities, such as: cross-site scripting (XSS), cross-site request forgery (CSRF), code injection vulnerabilities, and character encoding vulnerabilities. By using PHP's special functions and functions, such as: mysql_real_escape_string, etc., you can write safe code.

6 Code comments

Code comments are an important part of the code. You can know what the variable or function does through the code comments, which will be very useful in future code maintenance.

7 Use single quotes instead of double quotes

Strings always use single quotes instead of double quotes to avoid performance degradation caused by PHP searching for variables in the string. Use single quotes instead of double quotes to enclose the string, it will be faster to do so. Because PHP will search for variables in a string surrounded by double quotes, single quotes will not.

8 Escape string output

Use ENT_QUOTES as a parameter to pass to the htmlspecialchars function to ensure that single quotes (') are also converted into HTML entities, which is a good habit.

9 Use comma to separate string output

Outputting a string separated by a comma (,) through the echo statement has better performance than using the string concatenation operator (.).

10 Check the value passed before output

Check the passed value $_GET['query'] before outputting. Use isset or empty function, can be used to check whether the variable is a null value.

For more PHP-related technologies, please search Qianfeng PHP, be yourself, and educate with your conscience.

Guess you like

Origin blog.csdn.net/chengshaolei2012/article/details/72848063