Some experiences in PHP MySQL Development

Source: Internet
Author: User
Tags php mysql

The following tips is gradually formed in my actual work. Some of them may be incorrect. Some of them are out of personal habits. Therefore, do not consider the following tips as a standard, the huge bugs may be hidden, and the code may be running abnormally. SO! Please read it carefully and think about the benefits of doing so? What is the negative impact?

Development habits and PHP code
  1. Accurately understand various concepts. There are endless new things, which are harmful to the development work;
  2. Beautiful code, appropriate blank lines, indentation, and spaces, so that you can better 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 it. Even if you want to use off-the-shelf code, you need to review it in a row and then 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. In the development status, the project should be error_reporting (E_ALL ^ E_NOTICE ), when the Internet production environment is released, all error reports display_errors = Off should be closed. 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 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. Capturing exceptions with try and catch 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, so "$ {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. It's a pleasure to use sprintf formatting for language packs!
  14. Writing cache does not always require serialize once
  15. When Using AJAX to transmit data, do not directly send the arrays identified by the database to the client after json_encode. This not only poses certain security risks (field names are exposed ), in addition, some unnecessary data is transferred out to waste bandwidth. This is also applicable to API interfaces.
  16. Remember to handle the 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. It is not easy to 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 usually retry 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 path to find files
  23. Autoload is flexible.
  24. It is best to use set_error_handler and set_exception_handler, which makes 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
MySQL Section
  1. The SQL statement uses double quotation marks. The values in the SQL statement are enclosed in single quotation marks. For example, "INSERT INTO gril SET money = '{$ iMaxMoney}', age = '18 ′"
  2. Replace mysql extension with mysqli Extension
  3. Use mysqli_real_escape_string and mysqli_escape_string to process variables in outgoing SQL statements.
  4. Replace query "set names" with mysqli_set_charset (mysqli-> set_charset"
  5. Before JOIN, consider the data volume of each table. If not, separate queries should be performed, especially when cache is available.
  6. In many cases, the occurrence time needs to be recorded, but not every table needs to. Similarly, not every table needs an auto increment as the primary key.
  7. In many cases, it is good to add unsigned to the integer type.
  8. Inert deleyed, insert ignore, select distinct... This statement usually has unexpected results.
  9. The varchar type cannot 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.

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.