Php optimization and efficient speed-up problem summary _ PHP Tutorial

Source: Internet
Author: User
Summary of php optimization and efficient speed-up. 1. using return in a function to pass an array is more efficient than using global, for example: functionuserloginfo ($ usertemp) {$ detailexplode (|, $ usertemp); return $ detail ;} $ loginuserl 1. in a function, it is more efficient to use return when passing an array than to use 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
II. (this code is used to obtain the URL corresponding to the program directory, which is recommended)
$ 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. when judging in a loop, it is more efficient to use constant to determine the value.
$ A = 2; $ B = 2;
For example
If ($ a ==$ B) $ c = $;
Ratio
If ($ a ===$ B) $ c = $;

Efficient
4. use where in to use less limit for mysql queries
Limit queries the first few records of multiple records, which is fast, but the query speed is slow
Using in. the query of the continuity record is very fast. the first running of the non-continuity record will be a little slower, but it will be faster later!
V. NT Server data operation stability is less than unix/linux
6. use ob_start () whenever possible before output. it can accelerate the output speed. it is applicable to NT or nuli/linux. if ob_start ('OB _ gzhandler') is used for unlix servers '); higher output efficiency
7. try to use if ($ a = his value) when determining whether to use if (empty ($ a) whenever possible, because the program runs faster.


8. usage time! = Equivalent to <> efficiency
9. the efficiency of using $ a = "11111111111111" in personal experience is quite different from that in $ a = '000000 '.
10. using standard SQL statements will facilitate MySQL parsing
11. usage
If ($ online ){
$ Online1 = $ online;
Setcookie ('online1', $ online, $ cookietime, $ ckpath, $ ckdomain, $ secure );
}

COOKIE will take effect immediately
Use
If ($ online)

Using return in functions is more efficient than using global when passing arrays, for example: function userloginfo ($ usertemp) {$ detail = explode (|, $ usertemp ); return $ detail;} $ login = userl...

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.