Home  >  Article  >  Backend Development  >  Some experiences in PHP MySQL development_PHP tutorial

Some experiences in PHP MySQL development_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:33:42755browse

The following tips are slowly formed by me in actual work. Some may be incorrect, and some are due to personal habits. Therefore, do not regard the following tips as any standards, as they may be hidden. There are huge bugs, and the code may be running abnormally. SO! Please read it carefully and think about it. What are the benefits of doing this? What kind of negative impact will it have?

Development habits and PHP code

  1. Accurately understand various concepts. There are so many new things coming out in an endless stream now, and using literal meaning and a little knowledge is harmful to development work;
  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 comment them appropriately, 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. Don’t 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 easy to make mistakes. For large pieces of code such as open source classes, More needed;
  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 should be error_reporting( E_ALL ^ ​​E_NOTICE ) when it is in development status. When it is released to the external network production environment, all should be closed. Error reporting display_errors=Off, error_reporting(0) Netizen pAUL gAO shared their more reasonable solution, error_reporting(E_ALL | E_STRICT), and records error logs in the production environment.
  8. Record some necessary error logs, such as file writing failure, memcache writing failure, socket connection failure, reading and writing database failure. Logs can help quickly locate problems when problems occur. In external production environments, I personally strongly recommend turning off all error reports. of;
  9. Using try and catch to catch exceptions is helpful for the robustness of the code. It is often encountered in API interfaces, which makes it more friendly;
  10. It is recommended to add curly brackets to variables appearing in double quotes. As for whether it is "${nider}at gmail.com" or "{$tom}at zendstudio.net", it depends on personal habits. I prefer the latter one;
  11. Use as few if else nesting levels as possible. Maybe you have to express a very complex logical algorithm, but doing so can at least make the code logic clearer
  12. Read more excellent code from online open source projects (not open source code from excellent projects) and learn from it what is worth learning from
  13. How pleasant it is to use sprintf formatting for language packs!
  14. Write cache does not always need to be serialized first
  15. When AJAX transmits data, do not directly json_encode the array found in the database and then pass it to the client. Doing so not only has certain security risks (field names are exposed), but also wastes bandwidth by transmitting some unnecessary data. This also applies to API interfaces
  16. Remember to handle the magic variable. My method is to close it directly. 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 die of the program easily, especially inside the method
  19. require, require_once, include, include_once have slightly different application scenarios
  20. In order to maximize the success of writing to the cache, you can combine the number of retries + usleep. I usually retry 3 times. If it still doesn’t work, write down a log
  21. PHP’s constants are a very good thing. Many open source projects use an entire file to define the constants to be used
  22. Use absolute paths when 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 will make your project more perfect
  25. PHP’s reference types are very efficient and it is recommended to use
  26. when performing complex operations.
  27. The @ symbol suppressing errors is very performance-intensive, so find alternatives if possible

MySQL part

  1. SQL statements use double quotes, and the values ​​in them use single quotes, for example "INSERT INTO gril SET money='{$iMaxMoney}',age='18′"
  2. Use mysqli extension instead of mysql extension
  3. Use mysqli_real_escape_string and mysqli_escape_string to handle variables in outgoing sql statements
  4. Use mysqli_set_charset(mysqli->set_charset) instead of query "SET NAMES"
  5. Before joint query (JOIN), consider the data volume of each table. If it is not suitable, you should check it separately, especially when a cache is available
  6. Many places need to record the occurrence time, but not every table needs it. Similarly, not every table needs an auto-increment as the primary key
  7. Many times it is good to add unsigned to integer types
  8. INERT DELEYED, INSERT IGNORE, SELECT DISTINCT... statements like this usually have unexpected good effects
  9. The varchar type does not have a length that cannot exceed 255, but if it exceeds 255, this field cannot be indexed, so it depends on your actual needs

That’s all I can think of for now, I’ll continue to update when I think of more. 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752424.htmlTechArticleThe following tips are slowly formed by me in actual work. Some may be incorrect and some may be incorrect. It depends on personal habits, so please do not regard the following items as any standards, among which...
Statement:
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