In-depth discussion on how to improve PHP Running Efficiency

Source: Internet
Author: User

Everyone knows,Improve PHP running efficiency 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

Improve PHP running efficiency 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

Improve PHP running efficiency 3,

When judging in a loop, it is more efficient to use constant to determine the value.

 
 
  1. $a=2;$b=2;  

For example

 
 
  1. if($a==$b)$c=$a;   

Ratio

 
 
  1. if($a===$b)$c=$a;  

Efficient

Improve PHP running efficiency 4,

Use where in for mysql queries with less limit

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!

Improve PHP running efficiency 5,

The data operation stability of the NT Server is inferior to that of the unix/linux server.

Improve PHP Running Efficiency 6,

Use ob_start () whenever possible before output. It can accelerate the output speed. It is applicable to NT or nuli/linux. For unlix servers, if ob_start ('ob _ gzhandler') is used, the output efficiency will be higher.

Improve PHP running efficiency 7,

If ($ a = his/her value) is used whenever possible during the determination, if (empty ($ a) is used whenever possible because the program runs faster.

Improve PHP Running Efficiency 8,

Usage time! = Equivalent to <> Efficiency

Improve PHP running efficiency 9,

In my personal experience, the efficiency of using $ a = "11111111111111"; is quite different from that of $ a = '000000 '.

Improve PHP running efficiency by 10,

Using standard SQL statements will facilitate MySQL parsing.

Improve PHP running efficiency 11,

Use

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

COOKIE will take effect immediately

Use

 
 
  1. if($online)   
  2. setcookie('online1',$online,
    $cookietime,$ckpath,$ckdomain,$secure);  

The COOKIE must be refreshed again to take effect.

Improve PHP running efficiency 12,

Use

 
 
  1. $handle=fopen($filename,wb);   
  2. flock($handle,LOCK_SH);   
  3. $filedata=fread($handle,
    filesize($filename));   
  4. fclose($handle);  

Ratio

 
 
  1. file($filename);  

Excellent in both speed and stability

Improve PHP Running Efficiency 13, truncation string optimization functions (Can you avoid? Characters)

 
 
  1. ifunction substrs($content,$length) {   
  2. if(strlen($content)>$length){   
  3. $num=0;   
  4. for($i=0;$i<$length-3;$i++) {   
  5. if(ord($content[$i])>127)$num++;   
  6. }   
  7. $num%2==1 ? $content=substr(
    $content,0,$length-4):$content
    =substr($content,0,$length-3);   
  8. $content.=' ...';   
  9. }   
  10. return $content;   
  11. }   

For example

 
 
  1. $newarray[1]=substrs($newarray[1],25); 

Improve PHP running efficiency 14, shielding the case in the program

 
 
  1. For ($ asc = 65; $ asc <= 90; $ asc ++)
  2. {// Strtolower () this function is available in some services
    Garbled!
  3. If (strrpos ($ regname, chr ($ asc ))! = False)
  4. {
  5. $ Error = "to avoid user name confusion
    Uppercase letters are not allowed. Please Use lowercase letters ";
  6. $ Reg_check = 0;
  7. }
  8. }

Improve PHP running efficiency by 15. Do not use file (); or do not use fget (); (unstable or slow) Get an array of functions

 
 
  1. function openfile($filename,$method="rb")   
  2. {   
  3. $handle=@fopen($filename,$method);   
  4. @flock($handle,LOCK_SH);   
  5. @$filedata=fread($handle,filesize
    ($filename));   
  6. @fclose($handle);   
  7. $filedata=str_replace("n","
    n<ofstar:>",$filedata);   
  8. $filedb=explode("<ofstar:>",$filedata);   
  9. //array_pop($filedb);   
  10. $countcount=count($filedb);   
  11. if($filedb[$count-1]==''){unset
    ($filedb[$count-1]);}   
  12. return $filedb;   
  13. }  

Although this function has a lot of code, it has a great advantage in improving PHP running efficiency in terms of 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.