Tips Summary in PHP development

Source: Internet
Author: User
The following small series for everyone to bring a summary of some PHP development tips (must read). Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

First, development habits and PHP code

1, accurate understanding of various concepts. New things are emerging, words too literally and smattering are not good for development work;//For example, I come across someone who understands that the concept of loose coupling (this thing is not new) is to ask the code not to have a blank line, otherwise your program is "too Loose"

2, the code is beautiful, the appropriate blank line, indentation, space, so that it is easier to understand the meaning of the code snippet;

3, must write the note, but also to appropriate comments, otherwise the maintenance work or take over the code of the person will weep unceasingly;

4, static methods, class access rights, interfaces, abstract classes should be combined to use, play their respective characteristics;

5, do not copy and paste, even if you want to use out-of-the-box code, but also a line of review, then add to the new project, because experience tells us, this is too easy to make mistakes, for the use of open source class this large piece of code is more necessary;

6, the variables should be initialized;

7, do not only deal with error, and ignore warning and NOTICE, which may lead to future inexplicable problems, the project in the development state should be error_reporting (e_all ^ e_notice), wait until the release of the external network production environment, All error reports should be closed display_errors=off,error_reporting (0)

8, record some necessary error logs, such as write file failure, write memcache failure, socket connection failure, read and write database failure, log can help the problem of rapid positioning, external production environment I personally strongly recommend closing all error reports;

9, with try, catch catch exception, to the robustness of the code to help, often encountered in the API interface, this looks more friendly;

10, double quotation marks appear in the variable proposal with curly braces, as for "${nider}at" or "{$tom}at" Look at personal habits, I prefer the latter one;

11, as little as possible if else nesting layer, perhaps you want to express a very complex logic algorithm, but this can at least make the code logic clearer

12. Read the excellent code of the Open source project (not the open source code of excellent project), and draw the place which is worth learning from.

13, Language Pack sprintf format to do is how comfortable a thing ah!

14, write cache does not always have to serialize first time

15, when the AJAX data, do not put the database isolated directly after the Json_encode to the client, so do not only have a certain security risk (field name exposure), and some unnecessary data is sent to waste bandwidth, this same applies to API interface

16, to remember to deal with magic variables, my method is to close directly, of course, can also get the switch state to avoid the transfer of data processing two times the problem

17. Replace global $var with $globals[\ ' var\ ')

18, can not easily die off the program, especially in the method of internal

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

20, in order to maximize the success of the write cache, can be combined with the number of retries +usleep, I generally retry 3 times, not yet, then write down a log.

21, PHP constant is a very good thing, many open source projects with a whole file to define the constants to use

22, as far as possible to use absolute path to find files

23, AutoLoad is a very flexible thing

24, the best use of Set_error_handler and Set_exception_handler, it appears that your project is more perfect

25, PHP Reference type is very efficient, in the complex operation of the proposed use of

26, @ symbol suppression error is very performance-intensive, so as far as possible to find alternative solutions

Second, MySQL part

1, the SQL statement with double quotation marks, where the values are in single quotation marks, for example "INSERT into Gril SET money= ' {$iMaxMoney} ', age= ' 18′"

2. Replace MySQL extension with mysqli extension

2. Handling variables in outgoing SQL statements with mysqli_real_escape_string and mysqli_escape_string

3. Replace query "set NAMES" with Mysqli_set_charset (Mysqli->set_charset)

4, Joint query (join), consider the data volume of the table, inappropriate words should be checked separately, especially when the cache is available

5, many places need to record the occurrence time, but not every table needs, again, not every table needs a self-increment as the primary key

6, many times for the integer type plus unsigned is very good

7, INERT deleyed, INSERT IGNORE, SELECT DISTINCT ... This kind of statement usually has unexpected good effect

8, the varchar type is not not more than 255 length, but more than 255, this field can not be indexed, so, look at your actual needs

Think of so much for the time being, and then continue to update it. Think of what to write, there is no reason, a lot of forgiveness, if these have a bit of help to you, then I feel very happy.

The final piece of advice is to communicate more quickly and with others. Welcome to communicate with me, leave your valuable comments.

Related Article

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: 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.