PHP problem summary: Summary of PHP optimization and efficient Acceleration

Source: Internet
Author: User
PHP problem summary: Summary of PHP optimization and efficient acceleration-Linux Enterprise Application-Linux server application information. The following is a detailed description. 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 if it is negative, because empty ($ a) is 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)
Setcookie ('online1', $ online, $ cookietime, $ ckpath, $ ckdomain, $ secure );
  
The COOKIE must be refreshed again to take effect.
  
   12. Usage
  
$ Handle = fopen ($ filename, wb );
Flock ($ handle, LOCK_SH );
$ Filedata = fread ($ handle, filesize ($ filename ));
Fclose ($ handle );
  
Ratio
  
File ($ filename );
  
Excellent in both speed and stability
  
   13. truncation of string optimization functions (can be avoided? Characters)
  
Function substrs ($ content, $ length ){
If (strlen ($ content)> $ length ){
$ Num = 0;
For ($ I = 0; $ I <$ length-3; $ I ++ ){
If (ord ($ content [$ I])> 127) $ num ++;
}
$ Num % 2 = 1? $ Content = substr ($ content, 0, $ length-4): $ content = substr ($ content, 0, $ length-3 );
$ Content. = '...';
}
Return $ content;
}
  
For example, $ newarray [1] = substrs ($ newarray [1], 25 );
  
   14. case-insensitive shielding in the program
  
For ($ asc = 65; $ asc <= 90; $ asc ++)
{// Strtolower () This function generates garbled characters on some servers!
If (strrpos ($ regname, chr ($ asc ))! = False)
{
$ Error = "to avoid user name confusion, uppercase letters are not allowed in the user name. Please Use lowercase letters ";
$ Reg_check = 0;
}
}
  
   15. Do not use file (); or do not use fget (); (unstable or slow) take an Array Function
  
Function openfile ($ filename, $ method = "rb ")
{
$ Handle = @ fopen ($ filename, $ method );
@ Flock ($ handle, LOCK_SH );
@ $ Filedata = fread ($ handle, filesize ($ filename ));
@ Fclose ($ handle );
$ Filedata = str_replace ("\ n", "\ n", $ filedata );
$ Filedb = explode ("", $ filedata );
// Array_pop ($ filedb );
$ Count = count ($ filedb );
If ($ filedb [$ count-1] = '') {unset ($ filedb [$ count-1]);}
Return $ filedb;
}
  
Although this function has a lot of code, it has great advantages in 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.