PHP Run Efficiency summary (prompt program speed) _php Tutorial

Source: Internet
Author: User
Tags flock fread setcookie
1, in the function, when passing the array
Using return is more efficient than using global
Like what

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
2, (this code is used to get the program directory corresponding URL, recommended)
$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 more
$pre _urlarray=explode ('/', $HTTP _server_vars[' http_referer ');
$pre _url=array_pop ($pre _urlarray);
To be efficient
3, when judging in the loop, the numerical judgment uses the identity to be equal to the efficiency
$a =2; $b = 2;
Like what
if ($a = = $b) $c = $a;
Than
if ($a = = = $b) $c = $a;
Efficient
4,mysql use where in less limit when querying
Limit check many records of the first few, fast, but the query the most face will be slow
With in. On query continuity records, very fast, non-continuity records run slightly slower for the first time, but then faster!
5,NT Server data operation stability is less than unix/linux
6, use Ob_start () as far as possible before output; Can speed up output, applicable NT or Nuli/linux, if use Ob_start (' Ob_gzhandler ') for Unlix Class Server, output efficiency will be higher
7, judge when try to use if ($a = = his value) negative when try to use if (empty ($a)), because this program runs faster
8, using Unequal! = is equivalent to <> efficiency
9, personal experience to use $a = "11111111111111"; The efficiency and $a = ' 11111111111111 '; Not quite as much as the book says.
10, the use of canonical SQL statements, will facilitate the parsing of MySQL
11, using
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);

The cookie needs to be refreshed again to take effect
12, using
$handle =fopen ($filename, WB);
Flock ($handle, lock_sh);
$filedata =fread ($handle, FileSize ($filename));
Fclose ($handle);
Than
File ($filename);
Be good at speed or stability
13, truncated string optimization function (can avoid the appearance of 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;
}

such as $newarray[1]=substrs ($newarray [1],25);
14, screen case in program
for ($asc =65; $ASC <=90; $ASC + +)
{//strtolower () This function is garbled on some servers!
if (Strrpos ($regname, Chr ($ASC))!==false)
{
$error = "In order to avoid user name confusion, the use of uppercase letters in the user name, please use lowercase letter";
$reg _check=0;
}
}


15, do not use file (), and do not use Fget ();(unstable or slow) to 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;
}
//This function, although the code is more, but in the speed and stability of a great advantage!

http://www.bkjia.com/phpjc/320811.html www.bkjia.com true http://www.bkjia.com/phpjc/320811.html techarticle 1, in functions, passing an array using return is more efficient than using global such as function Userloginfo ($usertemp) {$detail =explode ("|", $usertemp); return $detail;} $login =userloginfo (...

  • 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.