10 Good Habits of PHP code specification

Source: Internet
Author: User
Tags naming convention php framework

10 Good Habits of PHP code specification

PHP is known as dirty but Quick's programming language. Although the PHP program is not simple and beautiful for other programming language users, we can also make PHP's code look comfortable and elegant with some good programming habits. What are the methods? Here's the next.

1. Plan your Code structure

Good PHP code should have a clear structure. PHP Object-oriented features allow programmers to decompose applications into functions or methods. If the code is obscure, you can also add comments to make the code function at a glance. You should try to separate the front-end code (HTML/CSS/JAVASCRIPT) from the service-side rules of your application, or you can build your application using a PHP framework that follows the MVC pattern.

2. Unified coding Style

Good PHP code should have a unified style. For example, a uniform naming convention for variables and functions, a uniform access standard for cyclic tasks (such as database access, error handling), or a regular code indentation can make it easier for others to read the code.

3. Portability

Good PHP code should be portable. Programmers should learn to use PHP's existing features (such as magic quotes and short tags, etc.), should understand the product requirements, to adapt to the characteristics of PHP, to ensure that the written PHP code is portable and cross-platform.

4. Code Security

Good PHP code should be secure. PHP5 has superior features and flexibility, but application security is often in the hands of programmers. As a professional PHP developer, there should be some in-depth knowledge of security vulnerabilities, such as cross-site scripting attacks (XSS), cross-site request forgery (CSRF), code injection vulnerabilities, and character encoding vulnerabilities. Using specific features and functions in PHP (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 running the function, which provides useful help in subsequent maintenance of the code.

6. Avoid shorthand marks

The full start tag should be used, and the shorthand start tag is not recommended.

7. Use single quotation marks instead of double quotation marks

Because PHP searches for variables in double quotes, programmers should use single quotes to refer to strings in order to avoid the performance impact of such searches.

8. Escape output

The ent_quotes parameter should be used in the Htmlspecialchars function to ensure that the single quotation mark (') can also be escaped. This is a good habit, although it is not required to do so.

9. Separating string output with commas

The string Connector (.) Can pass a single string to the Echo statement for output, compared with a comma that implements the separate output of strings in the Echo statement, which is a performance improvement for PHP.

10. Check the pass-through value before output

You should remember to check the pass value of $_get[' query ' before output. Use the Isset function or the empty function to check if the value of the variable is empty.

  • Related Article

    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.