How do I write good PHP code? ____php

Source: Internet
Author: User
Tags php framework
Writing good code is an art. If a programmer follows some good programming habits, then 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. Building good coding habits and improving design factors like modularity can make your code easier to understand, so it's easier and cheaper to maintain. Poor 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 bugs in your code.

1-Writing Modular Code

Good PHP code should be modular code. The object-oriented programming capabilities of PHP are particularly powerful tools that can decompose your application into functions or methods. You should try to separate the front-end Html/css/javascript code as much as possible from the server side of your application. You can also follow MVC (model-View-controller) patterns on any PHP framework.

2-Code Coding specification

Good PHP code should have a complete set of code coding specifications. You can make your code more readable by naming variables and functions, using a unified approach to accessing the database and handling errors, and the same code indentation to achieve the programming specification.

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 allows code to be independent and portable by adapting to PHP features.

4-Writing security Code

Good PHP code should be safe. PHP5 delivers excellent performance and flexibility. But the security issue is entirely about developers. For a professional PHP developer, an in-depth understanding of critical vulnerabilities is critical, such as Cross-site scripting (XSS), cross-site request forgery (CSRF), code injection vulnerabilities, and character encoding vulnerabilities. By using PHP's special features and functions, such as: mysql_real_escape_string, you can write safe code.

5-code Comment

Code comments are an important part of your code. The code annotation lets you know what the variable or function does, which will be useful in future code maintenance.

6-Avoid short labels

Replace all the short labels with the full PHP tags.

7-use single quotes instead of double quotes

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

8-Escape String output

It is a good practice to pass the ent_quotes argument to the Htmlspecialchars function to ensure that single quotes (') are also converted into HTML entities.

9-Separate string output with commas

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

10-Check the value of the output before

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

English original, oschina.net compilation

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.