PHP code optimization and PHP related issues summary

Source: Internet
Author: User
Tags count explode flock fread php code setcookie linux

1, 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

2, (this code is used to get the program directory corresponding 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

3, in the cycle of judgment, the value of the use of identity is equal to the efficient

$a=2;$b=2;

Like what

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

Than

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

Efficient

Use where in less limit when 4,mysql queries

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!

5,NT Server data operations are less stable than unix/linux

6, use Ob_start () as far as possible before using the output; 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

7, as far as possible use if ($a = = his value) to use if (empty ($a) when the negation, because the program runs faster

8, the use of unequal!= and <> efficiency is equivalent

9, personal experience to use $a = "11111111111111"; The efficiency and $a = ' 11111111111111 '; Quite. Not as much as the book says.

10, the use of standardized SQL statements, will be conducive to the analysis of MySQL

11, using

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);

Cookies need 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 and stability.

13, truncate the string optimization function (to avoid the occurrence of a 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;
}

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

14, the program in the case of shielding

for ($asc=65;$asc<=90;$asc++)
{ //strtolower() 此函数在一些服务器会产生乱码!
if (strrpos($regname,chr($asc))!==false)
{
$error="为了避免用户名混乱,用户名中禁止使用大写字母,请使用小写字母";
$reg_check=0;
}
}

15, do not use file (), and do not use Fget ();(instability or slow) to take 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);
 $count=count($filedb);
 if($filedb[$count-1]==''){unset($filedb[$count-1]);}
 return $filedb;
}
//这个函数虽然代码比较多,不过在速度和稳定性上优势很大!

Let's write this down.

The above completely personal summary, but the correctness has been repeatedly tested, if a friend questioned, please first Test, then discuss, thank you!

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.