Php code optimization and php related title Summary

Source: Internet
Author: User
1. when passing an array in a function, application return is more efficient than application global, for example, functionuserloginfo ($ usertemp) {$ detail = explode ("|", $ usertemp ); return $ detail;} $ login = userloginfo ($ userdb); compared with func 1, when passing an array in a function
Application return is more efficient than application global
For example
  
Function userloginfo ($ usertemp ){
$ Detail = explode ('|', $ usertemp );
Return $ detail;
}
$ Login = userloginfo ($ userdb );
  
Ratio
  
Function userloginfo ($ usertemp ){
Global $ detail;
$ Detail = explode ('|', $ usertemp );
}
Userloginfo ($ userdb );
  
Efficient
  
2. (this code is used to obtain the URL corresponding to the program directory and is recommended for applications)
  
$ Urlarray = explode ('/', $ HTTP_SERVER_VARS ['request _ URI ']);
$ Urlcount = count ($ urlarray); unset ($ urlarray [$ urlcount-1]);
$ Ofstarurl = 'http: // '. $ HTTP_SERVER_VARS ['http _ host']. implode ('/', $ urlarray );
  
Code comparison
  
$ Pre_urlarray = explode ('/', $ HTTP_SERVER_VARS ['http _ referer']);
$ Pre_url = array_pop ($ pre_urlarray );
  
Efficient
  
3. in the cycle, it is more efficient to use constant for numerical determination.
$ A = 2; $ B = 2;
For example
If ($ a ==$ B) $ c = $;
Ratio
If ($ a ===$ B) $ c = $;
Efficient
  
4. try to use where in for mysql Query with less limit
Limit queries the first few records of multiple records, which is fast, but the query speed is slow
Application in. the query of persistent records is very fast. the first running of non-persistent records will be a little slower, but it will be faster later!
  
5. NT Server data control is less stable than unix/linux
  
6. try to apply ob_start () to the application before output. it can speed up the output speed and be used in NT or nuli/linux. for unlix servers, if ob_start ('OB _ gzhandler') is used '); higher output efficiency
  
7. try to apply if ($ a = his value) when determining whether to apply if (empty ($ a) whenever possible, because the program runs faster.
  
8. application time is not equal! = Equivalent to <> efficiency
  
9. the efficiency of applying $ a = '000000' in personal experience is quite different from that in $ a = '000000 '.
  
10. the application of standardized SQL statements will facilitate MySQL parsing.
  
11. Application
  
If ($ online ){
$ Online1 = $ online;
Setcookie ('online1', $ online, $ cookietime, $ ckpath, $ ckdomain, $ secure );
}
  
COOKIE will take effect immediately
Application
  
If ($ online)
Setcookie ('online1', $ online, $ cookietime, $ ckpath, $ ckdomain, $ secure );
  
The COOKIE needs to be refreshed again to take effect.
  
12. Application
  
$ Handle = fopen ($ filename, wb );
Flock ($ handle, LOCK_SH );
$ Filedata = fread ($ handle, filesize ($ filename ));
Fclose ($ handle );
  
Ratio
  
File ($ filename );
  
Excellent in both speed and stability

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.