Everyone knows,Improve PHP running efficiency 1. When passing an array in a function
Using return is more efficient than using 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
Improve PHP running efficiency 2 (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
Improve PHP running efficiency 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=$a;
Ratio
- 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
- 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.
Improve PHP running efficiency 12,
Use
- $handle=fopen($filename,wb);
- flock($handle,LOCK_SH);
- $filedata=fread($handle,
filesize($filename));
- fclose($handle);
Ratio
- file($filename);
Excellent in both speed and stability
Improve PHP Running Efficiency 13, truncation string optimization functions (Can you avoid? Characters)
- ifunction 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);
Improve PHP running efficiency 14, shielding the case in the program
- For ($ asc = 65; $ asc <= 90; $ asc ++)
- {// Strtolower () this function is available in some services
Garbled!
- If (strrpos ($ regname, chr ($ asc ))! = False)
- {
- $ Error = "to avoid user name confusion
Uppercase letters are not allowed. Please Use lowercase letters ";
- $ Reg_check = 0;
- }
- }
Improve PHP running efficiency by 15. Do not use file (); or do not use fget (); (unstable or slow) Get an array of functions
- 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<ofstar:>",$filedata);
- $filedb=explode("<ofstar:>",$filedata);
- //array_pop($filedb);
- $countcount=count($filedb);
- if($filedb[$count-1]==''){unset
($filedb[$count-1]);}
- return $filedb;
- }
Although this function has a lot of code, it has a great advantage in improving PHP running efficiency in terms of speed and stability!