转载:优化PHP代码的40条建议

Filed Under 技术备案

转载自http://www.yeeyan.com/
原作者: Reinhold Weber 原文 译者: davidkoree

1.如果一个方法可静态化,就对它做静态声明。速率可提升至4倍。

2.echo 比 print 快。

3.使用echo的多重参数(译注:指用逗号而不是句点)代替字符串连接。

4.在执行for循环之前确定最大循环数,不要每循环一次都计算最大值。

5.注销那些不用的变量尤其是大数组,以便释放内存。

6.尽量避免使用__get,__set,__autoload。

7.require_once()代价昂贵。

8.在包含文件时使用完整路径,解析操作系统路径所需的时间会更少。

9.如果你想知道脚本开始执行(译注:即服务器端收到客户端请求)的时刻,使用$_SERVER[‘REQUEST_TIME’]要好于time()。

10.函数代替正则表达式完成相同功能。

11.str_replace函数比preg_replace函数快,但strtr函数的效率是str_replace函数的四倍。

12.如果一个字符串替换函数,可接受数组或字符作为参数,并且参数长度不太长,那么可以考虑额外写一段替换代码,使得每次传递参数是一个字符,而不是只写一行代码接受数组作为查询和替换的参数。

13.使用选择分支语句(译注:即switch case)好于使用多个if,else if语句。

14.用@屏蔽错误消息的做法非常低效。

15.打开apache的mod_deflate模块。

16.数据库连接当使用完毕时应关掉。

17.$row[‘id’]的效率是$row[id]的7倍。

18.错误消息代价昂贵。

19.尽量不要在for循环中使用函数,比如for ($x=0; $x < count($array); $x)每循环一次都会调用count()函数。

20.在方法中递增局部变量,速度是最快的。几乎与在函数中调用局部变量的速度相当。

21.递增一个全局变量要比递增一个局部变量慢2倍。

22.递增一个对象属性(如:$this->prop++)要比递增一个局部变量慢3倍。

23.递增一个未预定义的局部变量要比递增一个预定义的局部变量慢9至10倍。

24.仅定义一个局部变量而没在函数中调用它,同样会减慢速度(其程度相当于递增一个局部变量)。PHP大概会检查看是否存在全局变量。

25.方法调用看来与类中定义的方法的数量无关,因为我(在测试方法之前和之后都)添加了10个方法,但性能上没有变化。

26.派生类中的方法运行起来要快于在基类中定义的同样的方法。

27.调用带有一个参数的空函数,其花费的时间相当于执行7至8次的局部变量递增操作。类似的方法调用所花费的时间接近于15次的局部变量递增操作。

28.用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会。当然,只有当你不需要在字符串中包含变量时才可以这么做。

29.输出多个字符串时,用逗号代替句点来分隔字符串,速度更快。注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中说echo是语言结构,不是真正的函数,故把函数加上了双引号)。

30.Apache解析一个PHP脚本的时间要比解析一个静态HTML页面慢2至10倍。尽量多用静态HTML页面,少用脚本。

31.除非脚本可以缓存,否则每次调用时都会重新编译一次。引入一套PHP缓存机制通常可以提升25%至100%的性能,以免除编译开销。

32.尽量做缓存,可使用memcached。memcached是一款高性能的内存对象缓存系统,可用来加速动态Web应用程序,减轻数据库负载。对运算码 (OP code)的缓存很有用,使得脚本不必为每个请求做重新编译。

33. 当操作字符串并需要检验其长度是否满足某种要求时,你想当然地会使用strlen()函数。此函数执行起来相当快,因为它不做任何计算,只返回在zval 结构(C的内置数据结构,用于存储PHP变量)中存储的已知字符串长度。但是,由于strlen()是函数,多多少少会有些慢,因为函数调用会经过诸多步骤,如字母小写化(译注:指函数名小写化,PHP不区分函数名大小写)、哈希查找,会跟随被调用的函数一起执行。在某些情况下,你可以使用isset() 技巧加速执行你的代码。

(举例如下)
if (strlen($foo) < 5) { echo “Foo is too short”; }
(与下面的技巧做比较)
if (!isset($foo{5})) { echo “Foo is too short”; }

调用isset()恰巧比strlen()快,因为与后者不同的是,isset()作为一种语言结构,意味着它的执行不需要函数查找和字母小写化。也就是说,实际上在检验字符串长度的顶层代码中你没有花太多开销。

34. 当执行变量$i的递增或递减时,$i++会比++$i慢一些。这种差异是PHP特有的,并不适用于其他语言,所以请不要修改你的C或Java代码并指望它们能立即变快,没用的。++$i更快是因为它只需要3条指令(opcodes),$i++则需要4条指令。后置递增实际上会产生一个临时变量,这个临时变量随后被递增。而前置递增直接在原值上递增。这是最优化处理的一种,正如Zend的PHP优化器所作的那样。牢记这个优化处理不失为一个好主意,因为并不是所有的指令优化器都会做同样的优化处理,并且存在大量没有装配指令优化器的互联网服务提供商(ISPs)和服务器。

35.并不是事必面向对象(OOP),面向对象往往开销很大,每个方法和对象调用都会消耗很多内存。

36.并非要用类实现所有的数据结构,数组也很有用。

37.不要把方法细分得过多,仔细想想你真正打算重用的是哪些代码?

38.当你需要时,你总能把代码分解成方法。

39.尽量采用大量的PHP内置函数。

40.如果在代码中存在大量耗时的函数,你可以考虑用C扩展的方式实现它们。

41.评估检验(profile)你的代码。检验器会告诉你,代码的哪些部分消耗了多少时间。Xdebug调试器包含了检验程序,评估检验总体上可以显示出代码的瓶颈。

42.mod_zip可作为Apache模块,用来即时压缩你的数据,并可让数据传输量降低80%。

43.另一篇优化PHP的精彩文章,由John Lim撰写。

PHP5跨服务器使用session时保存方式的变化

Filed Under 技术备案

因为公司的一个小项目,需要在跨服务器环境下使用session。默认配置,session是以文件形式保存在服务器端,所以无法跨服务器使用。当然,PHP也提供了跨服务器的解决方案:session_set_save_handler()。考虑到效率,决定不使用通常将session存在数据库中的方法,改用memcache来保存session。

在PHP手册中,关于session_set_save_handler()有一个警告

As of PHP 5.0.5 the write and close handlers are called after object destruction and therefore cannot use objects or throw exceptions. The object destructors can however use sessions.It is possible to call session_write_close() from the destructor to solve this chicken and egg problem.

意思是而从PHP5.0.5开始,write和close处理函数的调用,是在所有对象被destruct之后的,这样就无法在这两个方法中使用对象和错误捕获了。从php5.0.5开始,在定制session保存方式时,就必须得用到session_write_close()了。

简单写了个使用memcache保存session数据的类,改成其他方式保存也是很简单的事。不过如果存在数据库中,是需要对数据进行一些处理的。memSession.rar

在这里顺便提一下PHP.net网站上的手册。跟下载到本地的chm不同,网站的手册,每个函数说明页面的下面,基本都有一大堆的note。这些note,都是大家在使用这个函数时遇到问题后的解决方法,或者是更深一层应用的扩展。其中的亮点很多,有时候都晃眼。比如在这个session_set_save_handler函数下面的notes中,就有使用NFS,Mysql,LDAP等保存session的代码。在php.net上查手册,有时候看notes比看函数说明更能让人明白函数的用法。所以,玩PHP,对于自己陌生的函数,只要能上网,严重推荐上php.net查手册。

行了,广告做完,洗洗睡了。

使用PKCS5填充的DES编码 PHP操作类库

Filed Under 技术备案

UUSEE的时候,因为工作需要,用PHP编写了这个使用“PKCS5填充的DES编码”的操作类库。
昨天在做一个小项目的时候刚好用到了,翻出来又做了一些更新。
这玩意比较生僻,放在这里算是个备份吧。
用法都很简单,就不详细写了,自己用的时候最好修改一些里头的参数,只要能看的懂应该就知道改啥:)

DES_PKCS5.rar

解决js在FireFox下不能复制文字到剪贴板的问题

Filed Under 技术备案

利用flash作为跳板来完成此功能,具体代码如下:

<script type="text/javascript">
    
function CopyText(id)
    
{
        
//copyToClipboard(document.getElementById(id).value);
        
copy(document.getElementById(id).value);
    
}
 
    
function copy(text2copy) {
        
if (window.clipboardData) {
            
window.clipboardData.setData("Text",text2copy);
        
} else {
            
var flashcopier = 'flashcopier';
            
if(!document.getElementById(flashcopier)) {
                
var divholder = document.createElement('div');
                
divholder.id = flashcopier;
                
document.body.appendChild(divholder);
            
}
            
document.getElementById(flashcopier).innerHTML = '';
            
var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';//这里是关键
            
document.getElementById(flashcopier).innerHTML = divinfo;
            
alert('Text copied');
        
}
    
}
 
    
function copyToClipboard(meintext)
    
{
        
if (window.clipboardData){
            
alert("ie");
            
// the IE-manier
            
window.clipboardData.setData("Text", meintext);
            
// waarschijnlijk niet de beste manier om Moz/NS te detecteren;
            
// het is mij echter onbekend vanaf welke versie dit precies werkt:
        
}
        
else if (window.netscape){
            
// dit is belangrijk maar staat nergens duidelijk vermeld:
            
// you have to sign the code to enable this, or see notes below
            
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
            
// maak een interface naar het clipboard
            
var clip = Components.classes['@mozilla.org/widget/clipboard;1'].
                        
createInstance(Components.interfaces.nsIClipboard);
            
if (!clip) return;
            
alert("mozilla");
            
// maak een transferable
            
var trans = Components.classes['@mozilla.org/widget/transferable;1'].
                        
createInstance(Components.interfaces.nsITransferable);
            
if (!trans) return;
            
// specificeer wat voor soort data we op willen halen; text in dit geval
            
trans.addDataFlavor('text/unicode');
            
// om de data uit de transferable te halen hebben we 2 nieuwe objecten
            
// nodig om het in op te slaan
            
var str = new Object();
            
var len = new Object();
            
var str = Components.classes["@mozilla.org/supports-string;1"].
                        
createInstance(Components.interfaces.nsISupportsString);
            
var copytext=meintext;
            
str.data=copytext;
            
trans.setTransferData("text/unicode",str,copytext.length*2);
            
var clipid=Components.interfaces.nsIClipboard;
            
if (!clip) return false;
            
clip.setData(trans,null,clipid.kGlobalClipboard);
        
}
        
alert("Following info was copied to your clipboard:\n\n" + meintext);
        
return false;
    
}
<
/script>

Linux下编译PHP时遇到的安装libmcrypt库的问题

Filed Under 技术备案

./configure –prefix=/usr/local/php-5.2.3 –enable-module=so –with-mysql=/usr/local/mysql-5.1.20 –with-apxs2=/usr/local/apache-2.2.4/bin/apxs –enable-mbstring –with-mcrypt

通过以上命令配置PHP时,遇到错误,总说找不到libmcrypt。由于这服务器是个裸机,很多必须的库都没装,必须一个一个的手动安装,可libmcrypt这个库我的确装过了。重试n遍,无解。在网上找了半天,终于在phpbuilder.com的一个maillist里发现一个哥们儿遇到的问题跟我差不多,按照他的方法做了一下,居然解决了。原文在这:http://www.phpbuilder.com/lists/php-install/2004032/0090.php

做为一个懒人,我就不逐句翻译了,摘抄解决办法如下:

——————————-
the solution
——————————-
Then I visited
http://mcrypt.hellug.gr/mcrypt/index.html
an learned that I have to install not only libmcrypt
but also mcrypt and libmhash.

Here is the to do:

libmcrytp:
———–
download libmcrypt-xxx.tar.gz
create the following directory: / usr / local / libmcrypt
copy the libmcrypt-xxx.tar.gz into that directory and move to it
run the following shell (>) commands: (’xxx’ is the version number)
> gunzip -c libmcrypt-xxx.tar.gz | tar xf -
> cd libmcrypt-xxx
> ./configure –disable-posix-threads
> make
> make check (note: ‘make check’ is optional)
> make install
then (update your environment) add in / etc / profile the following path:
/usr/local/libmcrypt/libmcrypt-xxxx
(note: as I run Red Hat 7.3 I set the line a f t e r the if-part
(id -u = 0 …) with: pathmunge /usr/local / libm….)
and add in / etc / ld.so.conf the following path: /usr/local/lib
then run ldconfig:
> ldconfig
now comes the important part:
> cd /usr/local/libmcrypt/libmcrypt-xxx/libltdl
> ./configure –enable-ltdl-install
> make
> make install

(maybe not needed: I also added a link in / usr / bin: )
(> cd /usr/bin)
(> ln -s /usr/lib/libltdl.so.3.1.0 ltdl)

mhash:
———–
download mhash-xxx.tar.gz
create the following directory: / usr / local / mhash
copy the mhash-xxx.tar.gz into that directory and move to it
run the following shell (>) commands: (’xxx’ is the version number)
> gunzip -c mhash-xxx.tar.gz | tar xf -
> cd mhash-xxx
> ./configure
> make
> make check (note: ‘make check’ is optional)
> make install

mcrypt:
———–
download mcrypt-xxx.tar.gz
create the following directory: / usr / local / mcrypt
copy the mcrypt-xxx.tar.gz into that directory and move to it
run the following shell (>) commands: (’xxx’ is the version number)
> gunzip -c mcrypt-xxx.tar.gz | tar xf -
> cd mhash-xxx
> ./configure
> make
> make check (note: ‘make check’ is optional)
> make install

back to PHP:
————–
move into your PHP installation directory
then the following command worked on my computer:
>
./configure –with-apache=/usr/local/apache/apache_1.3.29 –with-mysql –wit
h-mcrypt=/usr/local/mcrypt/mcrypt-2.6.4
> make
> make install

————–
that�s it

Hopefully that will help you to save some hours deep in the night :-)

打开了allow_url_fopen却不能远程访问URL

Filed Under 技术备案

今天发现以前帮别人写的一个信息采集类程序中的file和fopen函数在打开远程url时总是报错

Warning: file(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in xxxxxxx on line 26

在使用file,fopen或file_get_contents函数打开远程URL时,是需要PHP配置中(php.ini)将allow_url_fopen的值设置为On的。
先去检查这个,没有问题,按照默认设置,这个值本身就是On的。

在网上搜索了一下,出现这个问题,除了allow_url_fopen的值之外 ,还有就是服务器的DNS问题,造成了路由结果中有部份路由执行超时,最终PHP执行时间超时导致用到这类型的函数时报错。

联系朋友,在服务器上添加了几个新的DNS地址后,此问题解决:)