A brief summary of PHP optimization and high efficiency speed raising

Source: Internet
Author: User
Problem | optimization In a function, it is more efficient to use return than to use global when passing an array, for example:
  
function Userloginfo ($usertemp) {
$detail =explode ("|", $usertemp);
return $detail;
}
$login =userloginfo ($USERDB);
  
Than
  
function Userloginfo ($usertemp) {
Global $detail;
$detail =explode ("|", $usertemp);
}
Userloginfo ($USERDB);
  
To be efficient
  
Second, (this code is used to get the program directory corresponding to the URL, recommended use)
  
$urlarray =explode ('/', $HTTP _server_vars[' Request_uri '));
$urlcount =count ($urlarray); unset ($urlarray [$urlcount-1]);
$ofstarurl = ' http://'. $HTTP _server_vars[' http_host '].implode ('/', $urlarray);
  
This piece of code is better than
  
$pre _urlarray=explode ('/', $HTTP _server_vars[' http_referer '));
$pre _url=array_pop ($pre _urlarray);
  
To be efficient
  
Third, in the cycle of judgment, the value of the use of identity is equal to high efficiency
$a =2; $b = 2;
Like what
  
if ($a = = $b) $c = $a;
  
Than
if ($a = = = $b) $c = $a;
  
Efficient
  
Four, MySQL query when possible to use where in less use limit
  
Limit check more than a few records, the speed is very fast, but the most of the query will be slow
  
Use in. On query continuity record, very fast, discontinuous records first run will be slightly slower, but later will be faster!
  
NT Server data operation stability less than unix/linux
  
Vi. use Ob_start () as far as possible before the output is used; Can speed up output speed, suitable for NT or Nuli/linux, Unlix Class Server if Use Ob_start (' Ob_gzhandler '), output efficiency will be higher
  
Seven, when the judgement try to use if ($a = = his value) to use if (empty ($a), as far as possible, because the program runs faster.
  
Viii.!= and <> efficiency are equal when using unequal
  
Nine, personal experience to use $a = "11111111111111"; The efficiency and $a = ' 11111111111111 '; Quite. Not as much as the book says.
  
Ten, the use of standardized SQL statements, will be conducive to the analysis of MySQL
  
Xi. Use of
  
if ($online) {
$online 1= $online;
Setcookie (' Online1 ', $online, $cookietime, $ckpath, $ckdomain, $secure);
}
  
The cookie will take effect immediately.
Use
  
if ($online)
Setcookie (' Online1 ', $online, $cookietime, $ckpath, $ckdomain, $secure);
  
Cookies need to be refreshed again to take effect
  
12. Use
  
$handle =fopen ($filename, WB);
Flock ($handle, lock_sh);
$filedata =fread ($handle, FileSize ($filename));
Fclose ($handle);
  
Than
  
File ($filename);
  
Be good at speed and stability.
  
13, truncated string optimization function (to avoid the occurrence of the character)
  
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, the program screen case
  
For ($asc =65 $asc <=90; $ASC + +)
{//strtolower () This function will generate garbled characters on some servers!
if (Strrpos ($regname, Chr ($ASC))!==false)
{
$error = "In order to avoid user name confusion, the user name is forbidden to use uppercase letters, please use the lowercase letter";
$reg _check=0;
}
}
  
(), Do not use file (), and do not use Fget ();(unstable or slow) to fetch 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;
}
  
This function, although the code is more, but in the speed and stability of a great advantage!

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.