Summary of some tips in PHP development

Source: Internet
Author: User
Tags insert interface log mysql variables php code query variable

Development habits and PHP code

1, accurate understanding of various concepts. There are new things going on all the time, and words too literally and little knowledge are bad for the development work.//I've come across people who understand that loose coupling (this thing is not new) is the idea that the code should not have a blank line, otherwise your program is "too Loose"

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

3, must write the annotation, and must the proper annotation, otherwise the maintenance work behind or takes over the code person to be able to weep unceasingly;

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

5, do not copy and paste, even if you want to use the ready-made code, but also to a row of the review, and then add to the new project, because experience tells us, this is too easy to make mistakes, for the use of open source class such a large section of code more needed;

6. All variables should be initialized;

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

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

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

10, double quotes in the variable suggested by adding braces, as to "${nider}at gmail.com" or "{$tom}at zendstudio.net" to see personal habits, I prefer the back of A;

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

12, read more online open source project excellent code (not excellent project open source code), draw the place which is worth to learn from

13, the language package sprintf of the format to do is how comfortable thing ah!

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

15, Ajax data, do not json_encode the database detected directly after the array passed to the client, not only to have a certain security risks (field name exposure), and some unwanted data is spread out of wasted bandwidth, this is also applicable to API interface

16, remember to handle the magic variable, my method is directly closed, of course, can also get the switch state to avoid the transmission of data to be handled two times the problem

17, with $globals[\ ' var\ '] instead of global $var

18, can not easily die off the program, especially within the method

19, require, require_once, include, include_once have a 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 try again 3 times, not good then write down a log

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

22, as far as possible using the 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, so as far as possible to find alternatives

mysql section:

1, SQL statements in double quotes, where the values are in single quotes, such as INSERT into Gril SET money= ' {$iMaxMoney} ', age= ' 18′ '

2, with mysqli expansion instead of MySQL expansion

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

3, with Mysqli_set_charset (mysqli->set_charset) instead of query "set NAMES"

4, the Joint query (JOIN), consider the amount of data in each table, inappropriate words should be separately checked, especially when there is a cache available

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

6, most of the time 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, varchar type is not not can not exceed 255 length, but more than 255, this field can not be indexed, so, see your actual needs



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.