Summary of tips in PHP development

黄舟
Release: 2023-03-06 21:40:01
Original
1641 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template