PHP code optimization and PHP problems

Source: Internet
Author: User
1. when passing an array in a function: Using return is more efficient than using global, for example, functionuserloginfo ($ usertemp) {$ detailexplode (& quot; | & quot;, $ usertemp); return

1. when passing an array in a function:Using return is more efficient than using global, for example

  1. Function userloginfo ($ usertemp ){
  2. $ Detail = explode ("|", $ usertemp );
  3. Return $ detail;
  4. }
  5. $ Login = userloginfo ($ userdb );

Ratio

  1. Function userloginfo ($ usertemp ){
  2. Global $ detail;
  3. $ Detail = explode ("|", $ usertemp );
  4. }
  5. Userloginfo ($ userdb );

Efficient

2. (this code is used to obtain the URL corresponding to the program directory, which is recommended)

  1. $ Urlarray = explode ('/', $ HTTP_SERVER_VARS ['request _ URI ']);
  2. $ Urlcount = count ($ urlarray); unset ($ urlarray [$ urlcount-1]);
  3. $ Ofstarurl = 'http: // '. $ HTTP_SERVER_VARS ['http _ host']. implode ('/', $ urlarray );

Code comparison

  1. $ Pre_urlarray = explode ('/', $ HTTP_SERVER_VARS ['http _ referer']);
  2. $ Pre_url = array_pop ($ pre_urlarray );

Efficient

3. in the cycle, it is more efficient to use constant to determine the value.

  1. $ A = 2; $ B = 2;

For example

  1. If ($ a ==$ B) $ c = $;
  2. Ratio
  3. If ($ a ===$ B) $ c = $;
  4. Efficient

4. use where in to use less limit for mysql queries.

The speed of limit to query the first few records of multiple records is very fast, but it will be slow to query the first few records, using in. it is very fast to query the continuity record. the first running of the non-continuity record will be a little slower, but it will be faster later!

5. the data operation stability of the NT Server is less than that of unix/linux

6. use ob_start () whenever possible before output ();It can accelerate the output speed and is applicable to NT or nuli/linux. for unlix servers, if ob_start ('OB _ gzhandler') is used, the output efficiency will be higher.

7. try to use if ($ a = his value) if (empty ($ a) if it is negative )),Because the program runs faster

8. when not supported! = Equivalent to <> efficiency

9. in my personal experience, the efficiency of using $ a = "11111111111111"; is equivalent to that of $ a = '000000 '.It is not as different as the book says.

10. using standard SQL statements will facilitate MySQL parsing.

11. use

  1. If ($ online ){
  2. $ Online1 = $ online;
  3. Setcookie ('online1', $ online, $ cookietime, $ ckpath, $ ckdomain, $ secure );
  4. }

The COOKIE will take effect immediately.

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.