Some programming habits that make PHP code elegant

Source: Internet
Author: User

PHP is called the dirty but quick programming language. Despite the fact that PHP programs are not concise and elegant to other programming language users, we can use some excellent programming habits to make PHP code look more comfortable and elegant. What are the methods? Let's talk about it one by one.
1. Plan the code structure

Good PHP code should have a clear structure. PHP object-oriented features allow programmers to break down applications into functions or methods. If the code is obscure, you can add comments to make the code functions clear at a glance. When coding, try to separate the front-end code (html/CSS/JavaScript) from the application's server rules, or you can use the PHP framework following the MVC pattern to build your application.

2. Unified encoding Style

Excellent PHP code should have a uniform style. For example, develop unified naming rules for variables and functions, develop unified access standards for cyclic tasks (such as database access and error handling), or maintain regular code indentation, these coding habits make it easier for others to read the code.

3. Portability

Excellent PHP code should be portable. Programmers should learn to use existing PHP features (such as magic quotes and short tags), understand product requirements, adapt to PhP features, and ensure that written PHP code is portable and cross-platform.

4. Code Security

Excellent PHP code should be secure. PhP5 has excellent features and flexibility, but the security of applications is often in the hands of programmers. As a professional PHP developer, you should have a deep understanding of security vulnerabilities. Common Security Vulnerabilities include cross-site scripting (XSS) and Cross-Site Request Forgery (csrf), code injection and character encoding vulnerabilities. Using specific functions and functions in PHP (such as mysql_real_escape_string) can help programmers write secure code.

5. Add comments

Code comments are an important part of the code. They explain the purpose of function execution. Such comments can be very helpful for future code maintenance.
6. Avoid shorthand mark

The full start mark should be used. It is not recommended to use the short start mark.
7. replace double quotation marks with single quotes

Because PHP searches for the content in double quotation marks for variables, to avoid the performance impact of such searches, programmers should use single quotes to reference strings.
8. Escape output

The ent_quotes parameter should be used in the htmlspecialchars function to ensure that single quotation marks (') can also be escaped. Although there is no rule to do this, it is a good habit.
9. Use commas to separate string output

String connector (.) A single string can be passed to the echo statement for output. In contrast, a comma can implement separate output of strings in the ECHO Statement, which is a performance improvement for PHP.
10. Check transfer value before output

Check the transfer value of $ _ Get ['query'] Before output. Use the isset function or empty function to check whether the variable value is null.

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.