Heim >Backend-Entwicklung >PHP-Tutorial >深入探讨如何提高PHP运行效率_PHP教程

深入探讨如何提高PHP运行效率_PHP教程

WBOY
WBOYOriginal
2016-07-15 13:31:26890Durchsuche

大家都知道,提高PHP运行效率1,在函数中,传递数组时

使用 return 比使用 global 要高效

比如

  1. function userloginfo($usertemp){   
  2. $detail=explode("|",$usertemp);   
  3. return $detail;   
  4. }   
  5. $login=userloginfo($userdb);  

  1. function userloginfo($usertemp){   
  2. global $detail;   
  3. $detail=explode("|",$usertemp);   
  4. }   
  5. userloginfo($userdb); 

要高效

提高PHP运行效率2,(这个代码用于得到程序目录对应的网址,推荐使用)

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">urlarray</span><span>=</span><span class="attribute-value">explode</span><span>('/',$HTTP_<br>SERVER_VARS['REQUEST_URI']);   </span></span></li>
<li>
<span>$</span><span class="attribute">urlcount</span><span>=count($urlarray);unset<br>($urlarray[$urlcount-1]);   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">ofstarurl</span><span>=</span><span class="attribute-value">'http://'</span><span>.<br>$HTTP_SERVER_VARS['HTTP_HOST'].<br>implode('/',$urlarray);  </span>
</li>
</ol>

这段代码比

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">pre_urlarray</span><span>=</span><span class="attribute-value">explode</span><span>('/',$HTTP_SERVER_VARS['HTTP_REFERER']);   </span></span></li>
<li>
<span>$</span><span class="attribute">pre_url</span><span>=</span><span class="attribute-value">array_pop</span><span>($pre_urlarray); </span>
</li>
</ol>

要高效

提高PHP运行效率3,

在循环中判断时,数值判断使用恒等要比等于高效

<ol class="dp-xml"><li class="alt"><span><span>$</span><span class="attribute">a</span><span>=</span><span class="attribute-value">2</span><span>;$</span><span class="attribute">b</span><span>=</span><span class="attribute-value">2</span><span>;  </span></span></li></ol>

比如

<ol class="dp-xml"><li class="alt"><span><span>if($</span><span class="attribute">a</span><span>==$b)$</span><span class="attribute">c</span><span>=$a;   </span></span></li></ol>

<ol class="dp-xml"><li class="alt"><span><span>if($</span><span class="attribute">a</span><span>===$b)$</span><span class="attribute">c</span><span>=$a;  </span></span></li></ol>

高效

提高PHP运行效率4,

mysql 查询时尽量使用where in 少用 limit

limit查多记录的前几条, 速度很快, 但是查询最面几条就会慢

使用in .在查询连续性记录,非常快, 非连续性记录第一次运行会稍微慢一点,但是之后将比较快!

提高PHP运行效率5,

NT服务器数据操作稳定性不及unix/linux

提高PHP运行效率6,

输出前使用尽量使用 ob_start(); 可以加快输出速度,适用NT或nuli/linux,对unlix类服务器 如果使用 ob_start('ob_gzhandler');输出效率将更高

提高PHP运行效率7,

判断的时候尽量使用if($a==他的值) 否定的时候尽量使用if(empty($a)),因为这样程序运行更快速

提高PHP运行效率8,

使用不等时 != 与 效率相当

提高PHP运行效率9,

个人经验得 使用 $a="11111111111111"; 的效率和 $a='11111111111111'; 相当.并不象书本说的相差很大

提高PHP运行效率10,

使用规范的SQL语句, 会有利于MySQL的解析

提高PHP运行效率11,

使用

<ol class="dp-xml">
<li class="alt"><span><span>if($online){   </span></span></li>
<li>
<span>$</span><span class="attribute">online1</span><span>=$online;   </span>
</li>
<li class="alt"><span>setcookie('online1',$online,<br>$cookietime,$ckpath,$ckdomain,$secure);   </span></li>
<li><span>}  </span></li>
</ol>

COOKIE将马上生效

使用

<ol class="dp-xml">
<li class="alt"><span><span>if($online)   </span></span></li>
<li><span>setcookie('online1',$online,<br>$cookietime,$ckpath,$ckdomain,$secure);  </span></li>
</ol>

COOKIE需要再刷新一次才能生效

提高PHP运行效率12,

使用

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">handle</span><span>=</span><span class="attribute-value">fopen</span><span>($filename,wb);   </span></span></li>
<li><span>flock($handle,LOCK_SH);   </span></li>
<li class="alt">
<span>$</span><span class="attribute">filedata</span><span>=</span><span class="attribute-value">fread</span><span>($handle,<br>filesize($filename));   </span>
</li>
<li><span>fclose($handle);  </span></li>
</ol>

<ol class="dp-xml"><li class="alt"><span><span>file($filename);  </span></span></li></ol>

无论在速度还是稳定上都要优秀

提高PHP运行效率13,截断字符串优化函数(可避免?字符出现)

<ol class="dp-xml">
<li class="alt"><span><span>ifunction substrs($content,$length) {   </span></span></li>
<li>
<span>if(strlen($content)</span><span class="tag">></span><span>$length){   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">num</span><span>=</span><span class="attribute-value">0</span><span>;   </span>
</li>
<li>
<span>for($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><span>$length-3;$i++) {   </span></span>
</li>
<li class="alt">
<span>if(ord($content[$i])</span><span class="tag">></span><span>127)$num++;   </span>
</li>
<li><span>}   </span></li>
<li class="alt">
<span>$num%</span><span class="attribute">2</span><span>==1 ? $</span><span class="attribute">content</span><span>=</span><span class="attribute-value">substr</span><span>(<br>$content,0,$length-4):$</span><span class="attribute">content<br></span><span>=</span><span class="attribute-value">substr</span><span>($content,0,$length-3);   </span>
</li>
<li>
<span>$</span><span class="attribute">content.</span><span>=</span><span class="attribute-value">' ...'</span><span>;   </span>
</li>
<li class="alt"><span>}   </span></li>
<li><span>return $content;   </span></li>
<li class="alt"><span>}   </span></li>
</ol>

比如

<ol class="dp-xml"><li class="alt"><span><span>$newarray[1]=substrs($newarray[1],25); </span></span></li></ol>

提高PHP运行效率14,程序中屏蔽大小写

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

提高PHP运行效率15,不使用 file();和不使用 fget();(不稳定或速度慢) 取一数组函数

<ol class="dp-xml">
<li class="alt"><span><span>function openfile($filename,$</span><span class="attribute">method</span><span>=</span><span class="attribute-value">"rb"</span><span>)   </span></span></li>
<li><span>{   </span></li>
<li class="alt">
<span>$</span><span class="attribute">handle</span><span>=@fopen($filename,$method);   </span>
</li>
<li><span>@flock($handle,LOCK_SH);   </span></li>
<li class="alt">
<span>@$</span><span class="attribute">filedata</span><span>=</span><span class="attribute-value">fread</span><span>($handle,filesize<br>($filename));   </span>
</li>
<li><span>@fclose($handle);   </span></li>
<li class="alt">
<span>$</span><span class="attribute">filedata</span><span>=</span><span class="attribute-value">str_replace</span><span>("n","<br>n</span><span class="tag"><span class="tag-name">ofstar:</span><span class="tag">></span><span>",$filedata);   </span></span>
</li>
<li>
<span>$</span><span class="attribute">filedb</span><span>=</span><span class="attribute-value">explode</span><span>("</span><span class="tag"><span class="tag-name">ofstar:</span><span class="tag">></span><span>",$filedata);   </span></span>
</li>
<li class="alt"><span>//array_pop($filedb);   </span></li>
<li>
<span>$</span><span class="attribute">count</span><span class="attribute-value">count</span><span>=count($filedb);   </span>
</li>
<li class="alt"><span>if($filedb[$count-1]==''){unset<br>($filedb[$count-1]);}   </span></li>
<li><span>return $filedb;   </span></li>
<li class="alt"><span>}  </span></li>
</ol>

这个函数虽然代码比较多,不过在速度和稳定性上提高PHP运行效率的优势很大!


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446216.htmlTechArticle大家都知道, 提高PHP运行效率1,在函数中,传递数组时 使用 return 比使用 global 要高效 比如 functionuserloginfo($usertemp){ $ detail = explode (|,$userte...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn