How to write a good PHP code ?, Write php code _ PHP Tutorial

Source: Internet
Author: User
How to write a good PHP code ?, Write php code. How to write a good PHP code ?, Writing php code is an art of writing good code. If a programmer follows some good programming habits, then he can become a good PHP code ?, Write php code

Writing good code is an art. If a programmer follows some good programming habits, then he can become a good programmer. In fact, you are likely to spend more time on code maintenance than the time you write code; not to mention the maintenance of the entire application. Building good coding habits can improve design factors such as modularity, and your code will be easier to understand. Therefore, it is easier to maintain and lower cost. Poor coding habits may have defects in the code, and may cause the code to be difficult to maintain.

In this article, we will explore some good programming habits, which will help you avoid defects in the code.

1-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 the MVC (Model-View-Controller) mode in any PHP framework.

2-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.

3-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.

4-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.

5-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.

6-avoid short labels

Replace all short labels with the complete PHP labels.

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.

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 value 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.

Login PHP code ?, Writing php code is an art of writing good code. If a programmer follows some good programming habits, then he can become a good programmer...

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.