Top 10 excellent habits of PHP and top 10 php

Source: Internet
Author: User

Top 10 excellent habits of PHP and top 10 php

The details of php are as follows:

1. Read more manuals and source code

Nothing is more worth mentioning than reading the manual-you can learn a lot by reading the manual, especially a lot of functions related to strings and arrays. Many useful functions are included in these functions. if you carefully read the manual, you will often find that in the previous project development process, you often "reinvent the wheel ", in fact, you only need a core function to complete the corresponding functions. The manual is your friend. In addition, there are many open-source programs developed using PHP. Why not study and learn from it? Download the source code of an open-source PHP application. Read it carefully. Maybe the larger the project, the more worth reading. Although they may have more complex structures and systems, there are more detailed explanations.

2. Write modular code

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

3. code writing specifications

Good PHP code should have a complete set of coding specifications. By naming variables and functions, you can use uniform methods to access the database, handle errors, and indent the same code to achieve programming specifications. This makes your code more readable.

4. Write portable code

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

5. Write Security Code

Good PHP code should be safe. PHP5 provides outstanding performance and flexibility. However, the security problem lies completely with developers. For a professional PHP developer, it is critical to have a deep understanding of major security vulnerabilities, such as cross-site scripting (XSS) and Cross-Site Request Forgery (CSRF), code injection, and character encoding vulnerabilities. You can write Secure Code by using special PHP functions, such as mysql_real_escape_string.

6. Code comments

Code annotation is an important part of code. Through code annotation, you can know what the variable or function is, which will be very useful in future code maintenance.

7. Use single quotes instead of double quotes

The string always uses single quotes instead of double quotation marks to avoid performance degradation caused by PHP search for variables in the string. Use single quotes instead of double quotes to include strings, which is faster. Because PHP searches for variables in strings enclosed by double quotation marks

8. Escape string output

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

9. Use commas to separate string output

Using echo statements to output strings separated by commas (,) is better than using the String concatenation operator.

10. Check the sent values before output.

Check the passed value $ _ GET ['query'] Before output. The isset or empty function can be used to check whether the variable is null.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.