How to write good PHP code? , write PHP code _php tutorial

Source: Internet
Author: User
Tags php framework

How to write good PHP code? , write the PHP code


Writing good code is an art. If a programmer follows some good programming habits, he can become a good programmer. In fact, you'll probably spend more time on code maintenance than when you write code, not to mention the entire application maintenance. By building good coding habits that can improve design factors like modularity, your code will be easier to understand and therefore easier to maintain and less expensive. Bad coding habits can be flawed in your code and can cause code to be difficult to maintain.

In this article, we'll explore some good programming habits that will help you avoid flaws in your code.

1-Writing Modular Code

A good PHP code should be a modular code. PHP's object-oriented programming features are particularly powerful tools that can break down 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.

2-Code Writing specification

Good PHP code should have a complete code-writing specification. By naming the variables and functions, a unified approach to accessing the database and handling the errors, and the same code indentation, and so on to achieve the programming specifications, so that your code can be more readable.

3-Writing Portable Code

Good PHP code should be portable. You can use PHP's existing features, such as magic quotes and short tags. Try to understand your needs and then write code that is independent and portable by adapting to PHP features.

4-Writing security Code

Good PHP code should be safe. The PHP5 provides excellent performance and flexibility. But the security issue lies entirely with developers. For a professional PHP developer, deep understanding of critical security vulnerabilities is critical, such as cross-site scripting (XSS), cross-station request forgery (CSRF), Code injection vulnerability, and character encoding vulnerability. By using PHP's special functions and functions, such as: Mysql_real_escape_string and so on, you can write safe code.

5-code Comment

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

6-Avoid short labels

Replace all of the short tags with the full php tag.

7-use single quotation marks instead of double quotes

strings always use single quotes instead of double quotes to avoid performance degradation caused by variables within the PHP search string.

8-Escape String output

It is a good practice to use ent_quotes as a parameter to pass to the Htmlspecialchars function to ensure that single quotation marks (') are also converted to HTML entities.

9-separating string output with commas

Use the Echo statement to output a string separated by commas (,), rather than using the string join operator (.). Performance is better.

10-Check the value before output

Check the value passed before output $_get[' query ']. You can use the Isset or empty function to check whether a variable is a null value.

http://www.bkjia.com/PHPjc/1133577.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133577.html techarticle How to write good PHP code? , it is an art to write code that is well written in PHP code. If a programmer follows some good programming habits, then he can become an excellent ...

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