PHP running efficiency summary of of prompt program speed

  • 2020-03-31 19:23:32
  • OfStack

1, in the function, when you pass an array
Using return is more efficient than using global
Such as

The function userloginfo ($usertemp) {
$detail = explodes (" | ", $usertemp);
Return $detail;
}
$login = userloginfo ($userdb);
than
The function userloginfo ($usertemp) {
Global $detail;
$detail = explodes (" | ", $usertemp);
}
Userloginfo ($userdb);

To efficiently
2,(this code is used to get the corresponding url of the program directory, recommended to use)
$urlarray = explodes ('/', $HTTP_SERVER_VARS [' REQUEST_URI ']);
$urlcount = count ($urlarray); The unset ($urlarray $urlcount [1]).
$ofstarurl = 'http://'. $HTTP_SERVER_VARS [' HTTP_HOST] implode ('/', $urlarray);
This code ratio
$pre_urlarray = explodes ('/', $HTTP_SERVER_VARS [' HTTP_REFERER ']);
$pre_url = array_pop ($pre_urlarray);
To efficiently
3. When judging in the loop, the numerical judgment USES the identity ratio to be efficient
$a = 2; $b = 2;
Such as
If ($a = = $b) $c = $a;
than
If ($a = = = $b) $c = $a;
efficient
4. Use where in instead of limit for mysql queries
Limit is quick to look up the first few entries in a long list, but slow to look up the last few entries
Using in. In query for continuous records, very fast, discontinuous records run a little slower the first time, but faster later!
5,NT server data operation stability is not as good as Unix/Linux
6. Try to use ob_start() before output; You can speed up the output for NT or nuli/ Linux, for unlix servers if you use ob_start('ob_gzhandler'); Output efficiency will be higher
7, try to use if($a== his value) when judging, try to use if(empty($a)) when negating, because it will run faster
8, use unequal time! = with < > Efficiency is quite
9, personal experience to use $a=" 1111111111111111 "; The efficiency and $a='11111111111111'; Quite. Not so different as books say
10, the use of standard SQL statements, will be conducive to MySQL parsing
11, the use of
If ($online) {
$online1 = $online;
Setcookie (' online1, $online, $cookietime, $ckpath, $ckdomain, $secure);
}

The COOKIE will take effect immediately
use
If ($online)
Setcookie (' online1, $online, $cookietime, $ckpath, $ckdomain, $secure);

The COOKIE needs to be refreshed again to take effect
12, the use of
$handle = fopen ($filename, wb);
Flock ($handle, LOCK_SH);
$filedata = fread ($handle, filesize ($filename));
Fclose ($handle);
than
The file ($filename);
Be good at both speed and stability
13, truncate string optimization function (can avoid? Character occurrence)
The function substrs ($content, $length) {
If (strlen ($content) > ${length)
$num = 0;
For ($I = 0; $i< $length - 3; ${i++)
If (word ($content [$I]) > 127) $num++;
}
$num % 2 = = 1? $content = substr ($content, 0, $length - 4) : $content = substr ($content, 0, $length - 3);
$content. = '... ';
}
Return the $content;
}

Such as $newarray [1] = substrs ($newarray [1], 25).
14. Block case in the program
For ($asc = 65; $asc< = 90; $asc++)
{//strtolower() this function generates garbled code on some servers!
If (strrpos ($regname, CRH ($asc))! = = false)
{
$error=" in order to avoid confusion of user name, the user name is not allowed to use uppercase letters, please use lowercase letters ";
$reg_check = 0;
}
}


15, do not use file(); And not using fget(); (unstable or slow) take an array function
The 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 = explodes (" < Ofstar: >" , $filedata);
/ / array_pop ($filedb);
$count = count ($filedb);
If ($filedb [$count - 1] = = ' ') {unset ($filedb [$count - 1)); }
Return $filedb;
}
// although this function has more code, it has great advantages in speed and stability!

Related articles: