Home Backend Development PHP Tutorial Summary of tips in PHP development

Summary of tips in PHP development

Mar 25, 2017 am 10:39 AM

The following editor will bring you a summary of some tips in PHP development (a must-read article). The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

1. Development habits and php code

1. Accurate understanding Various concepts. Nowadays, new things are emerging in an endless stream. Literal meaning and a little knowledge are harmful to development work; //For example, I met someone who understood the concept of loose coupling (this thing is not new) and actually required that the code should not have blank lines, otherwise your program would be "too "Loose"

2. The code is beautiful, with appropriate blank lines, indentations, and spaces, so that it is easier to understand the meaning of the code segment;

3. Be sure to write comments, and they must be appropriate comments, otherwise the subsequent maintenance work or the person who takes over the code will cry bitterly;

4. Static methods, class access rights, interfaces, and abstract classes should be used together to give full play to their respective characteristics;

5. Do not copy and paste. Even if you want to use ready-made code, you must review it line by line before adding it to a new project, because experience tells us that this is too error-prone. For large sections such as open source classes, The code needs more;

6. Variables must be initialized;

7. Don’t just deal with errors and ignore warnings and notices. This may lead to inexplicable problems in the future. The project is in the development state The following should be error_reporting(E_ALL ^ ​​E_NOTICE). When the external network production environment is released, all error reports should be turned off display_errors=Off,error_reporting(0)

8. Record some necessary error logs, such as writing files Failure, failure to write to memcache, failure to connect to socket, failure to read and write to the database, logs can help quickly locate problems when problems occur. I personally strongly recommend turning off all error reports in external production environments;

9. Use try, catch captures exceptions, which is helpful for the robustness of the code. It is often encountered in the API interface, which makes it much more friendly;

10. It is recommended to add curly brackets to variables appearing in double quotes. As for "$ {nider}at gmail.com" or "{$tom}at zendstudio.net" depends on personal habits. I prefer the latter one;

11. Try to have as few if else nesting levels as possible. Maybe you It is necessary to express a very complex logical algorithm, but doing so can at least make the code logic clearer

12. Read more excellent codes of online open source projects (not open source codes of excellent projects), and learn from them what is worth learning from

13. How pleasant it is to use sprintf formatting for language packages!

14. When writing cache, you do not always need to serialize it first.

15. When AJAX transmits data, do not directly json_encode the array found in the database and then pass it to the client. Do this Not only is there a certain security risk (field names are exposed), but some unnecessary data is transmitted out and wastes bandwidth. This also applies to API interfaces

16. Remember to deal with magic variables. My method is to directly Close, of course, you can also get the switch status to avoid the problem of transmitting data being processed twice

17. Use $GLOBALS[\'var\'] instead of global $var

18. Don’t do it easily Die out the program, especially inside the method

19. require, require_once, include, and include_once have slightly different application scenarios

20. In order to maximize the success of the write cache, you can Combining the number of retries + usleep, I usually retry 3 times. If it still doesn’t work, write down a log.

21. PHP constants are a very good thing. Many open source projects use an entire file to define them. Constants to be used

22. Use absolute paths as much as possible to find files

23. Autoload is a very flexible thing

24. It is best to use set_error_handler and set_exception_handler , which makes your project more perfect

25. PHP’s reference type is very efficient. It is recommended to use

26 when performing complex operations. The @ symbol is very performance-intensive to suppress errors. Therefore, try to find alternatives as much as possible

2.MYSQLPart

1. Use double quotes in SQL statements, where The values ​​​​are all in single quotes, for example "INSERT INTO gril SET money='{$iMaxMoney}',age='18′"

2, use mysqli extension instead of mysql extension

2, Use mysqli_real_escape_string and mysqli_escape_string to process variables in outgoing sql statements

3. Use mysqli_set_charset(mysqli->set_charset) instead of query "SET NAMES"

4.Union query## Before #(JOIN), consider the amount of data in each table. If it is not appropriate, you should check it separately, especially when cache is available.

5. Many places need to record the occurrence time, but not every table needs it. , similarly, not every table needs an auto-increment primary key

6. Many times it is good to add unsigned to the

integer type

7. INERT DELEYED, INSERT IGNORE, SELECT DISTINCT...this kind of statements usually have unexpected good effects

8. The varchar type does not have a length that cannot exceed 255, but if it exceeds 255, this field cannot be indexed. Therefore, it depends on your actual needs.

That’s all I can think of for the time being. I’ll wait for more. Keep updating as you think of it. I write whatever comes to my mind, there is no structure, so please bear with me. If this helps you at all, then I will be very happy.

The last piece of ultimate advice is - communicate more with others to make faster and greater progress! Welcome to communicate with me and leave your valuable opinions.

The above is the detailed content of Summary of tips in PHP development. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles