Summary of tips in PHP development

Source: Internet
Author: User
The following is a summary of some PHP development tips ). I think this is quite good. now I will share it with you and give you a reference. Let's take a look at the following small series to bring you a summary of some PHP development tips (must read ). I think this is quite good. now I will share it with you and give you a reference. Let's take a look at it with Xiaobian.

I. Development habits and php code

1. accurately understand various concepts. The new things are emerging one after another, and it may be harmful to the development work. // for example, if someone understands loose coupling (this is not new), the concept of code should not be empty, otherwise, your program will be "too loose ".

2. the code is beautiful, with proper blank lines, indentation, and spaces. This makes it easier to understand the meaning of the code segment;

3. Be sure to write comments and make appropriate comments. Otherwise, the people who take over the code will cry;

4. static methods, class access permissions, interfaces, and abstract classes should be used in combination to give full play to their respective features;

5. do not copy and paste the code. even if you want to use the ready-made code, you must review the code line by line and add it to the new project. as experience tells us, this is too error-prone, it is more necessary to use large segments of code such as open-source classes;

6. all variables must be initialized;

7. do not only handle errors, but ignore warning and notice. This may cause inexplicable problems in the future. The project should be error_reporting (E_ALL ^ E_NOTICE) in the development status ), when releasing an Internet production environment, disable all error reports display_errors = Off and error_reporting (0)

8. record necessary error logs, such as file writing failure, memcache writing failure, socket connection failure, and database read/write failure. logs can help you quickly locate problems, in an external production environment, I personally strongly recommend that you close all error reports;

9. Using try and catch to catch exceptions is helpful for code robustness and is often encountered in API interfaces, which makes it much better;

10. we recommend that you add braces to variables that appear in double quotation marks. for example, "$ {nider} at gmail.com" or "{$ tom} at zendstudio.net, I prefer the latter one;

11. as few as possible if else nested layers, you may need to express a very complex logic algorithm, but doing so can at least make the code logic clearer.

12. read the excellent code of the open-source project on the Internet (not the open-source code of the excellent project) and learn from it.

13. How comfortable is the language pack to be formatted using sprintf!

14. writing cache does not always require serialize once

15. when using AJAX to transmit data, do not directly send the json_encode of the array identified by the database to the client. This not only poses a certain security risk (field name exposure ), in addition, some unnecessary data is transferred out to waste bandwidth. this is also applicable to API interfaces.

16. remember to handle magic Variables. my method is to turn them off directly. of course, you can also get the switch status to avoid the problem of data transmission being processed twice.

17. replace global $ var with $ GLOBALS [\ 'Var \ ']

18. you cannot easily die the program, especially inside the method.

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

20. to maximize the write success of the cache, you can combine the number of retries + usleep. I generally try again three times. if not, remember the next log.

21. PHP constants are a good thing. many open-source projects use an entire file to define constants to be used.

22. try to use absolute paths to find files.

23. autoload is a flexible thing

24. it is best to use set_error_handler and set_exception_handler to make your project more perfect.

25. PHP reference types are very efficient. we recommend that you use them for complex operations.

26. @ symbol suppression errors consume performance, so try to find an alternative solution.

II. MYSQL

1. double quotation marks are used in SQL statements, and the values in the statements are enclosed in single quotation marks. for example, "INSERT INTO gril SET money = '{$ iMaxMoney}', age = '18 ′"

2. replace mysql extension with mysqli extension

2. use mysqli_real_escape_string and mysqli_escape_string to process variables in outgoing SQL statements.

3. replace query "set names" with mysqli_set_charset (mysqli-> set_charset"

4. Before JOIN, consider the data volume of each table. if not, separate query should be performed, especially when cache is available.

5. the occurrence time needs to be recorded in many places, but not in every table. Similarly, not every table needs an auto increment as the primary key.

6. it is good to add unsigned to the integer type in many cases.

7. inert deleyed, insert ignore, select distinct... This statement usually has unexpected results.

8. the varchar type does not exceed 255 characters, but exceeds 255. Therefore, you cannot create an index for this field.

For the time being, I think so much. wait until I think about the update. There is no reason to think about what to write, and I am sorry for it. if it helps you a little bit, I feel very happy.

The last ultimate suggestion is to communicate with others more quickly and more quickly! Thank you for your valuable comments.

The above is the summary of tips in PHP Development. For more information, see other related articles in the first PHP 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: 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.