php great God's ten good habits _php example

Source: Internet
Author: User
Tags php framework

The PHP great god nurturance, the concrete content is as follows

1, read more manuals and source code

There is nothing more important than a reading manual – you can learn a lot by reading the manual, especially with a lot of functions about strings and arrays. In these functions, there are many useful functions, if you read the manual carefully, you will often find in the previous project development process, many times you are "reinvent the wheel", and in fact you only need a core function to complete the corresponding functions. The handbook is your friend. In addition, there are many open source programs that are developed using PHP. Why not study and learn from it? Download the source code for an open source PHP application and read it carefully. Perhaps the larger the project is worth reading, although they may have more complex structure and system, but also have a more detailed interpretation of the document.

2, the preparation of 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, and you can follow MVC (model-View-Controller) mode on any PHP framework.

3, Code coding specifications

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.

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

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

6, code comments

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.

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. It is quicker to use single quotes instead of double quotes to contain strings. Because PHP searches for variables in a string enclosed in double quotes, single quotes do not

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. Using commas to separate string output

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

10, the output before the check came from the value

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.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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