优化PHP代码的40条建议

原文地址:http://reinholdweber.com/?p=3

英文版权归Reinhold Weber所有,中译文作者yangyang(aka davidkoree)。双语版可用于非商业传播,但须注明英文版作者、版权信息,以及中译文作者。翻译水平有限,请广大PHPer指正。

1. If a method can be static, declare it static. Speed improvement is by a factor of 4. 如果一个方法可静态化,就对它做静态声明。速率可提升至4倍。

2. echo is faster than print. echo 比 print 快。

3. Use echo’s multiple parameters instead of string concatenation. 使用echo的多重参数(译注:指用逗号而不是句点)代替字符串连接。

4. Set the maxvalue for your for-loops before and not in the loop. 在执行for循环之前确定最大循环数,不要每循环一次都计算最大值。

5. Unset your variables to free memory, especially large arrays. 注销那些不用的变量尤其是大数组,以便释放内存。

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

7. require_once() is expensive require_once()代价昂贵。

8. Use full paths in includes and requires, less time spent on resolving the OS paths. 在包含文件时使用完整路径,解析操作系统路径所需的时间会更少。

9. If you need to find out the time when the script started executing, $_SERVER[’REQUEST_TIME’] is preferred to time() 如果你想知道脚本开始执行(译注:即服务器端收到客户端请求)的时刻,使用$_SERVER[‘REQUEST_TIME’]要好于time()。

10. See if you can use strncasecmp, strpbrk and stripos instead of regex. 检查是否能用strncasecmp,strpbrk,stripos函数代替正则表达式完成相同功能。

11. str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4. str_replace函数比preg_replace函数快,但strtr函数的效率是str_replace函数的四倍。

12. If the function, such as string replacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement statements, passing one character at a time, instead of one line of code that accepts arrays as search and replace arguments. 如果一个字符串替换函数,可接受数组或字符作为参数,并且参数长度不太长,那么可以考虑额外写一段替换代码,使得每次传递参数是一个字符,而不是只写一行代码接受数组作为查询和替换的参数。

13. It’s better to use select statements than multi if, else if, statements. 使用选择分支语句(译注:即switch case)好于使用多个if,else if语句。

14. Error suppression with @ is very slow. 用@屏蔽错误消息的做法非常低效。

15. Turn on apache’s mod_deflate 打开apache的mod_deflate模块。

16. Close your database connections when you’re done with them. 数据库连接当使用完毕时应关掉。

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

18. Error messages are expensive. 错误消息代价昂贵。

19. Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time. 尽量不要在for循环中使用函数,比如for ($x=0; $x < count($array); $x)每循环一次都会调用count()函数。

20. Incrementing a local variable in a method is the fastest. Nearly the same as calling a local variable in a function. 在方法中递增局部变量,速度是最快的。几乎与在函数中调用局部变量的速度相当。

21. Incrementing a global variable is 2 times slow than a local var. 递增一个全局变量要比递增一个局部变量慢2倍。

22. Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable. 递增一个对象属性(如:$this->prop++)要比递增一个局部变量慢3倍。

23. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one. 递增一个未预定义的局部变量要比递增一个预定义的局部变量慢9至10倍。

24. Just declaring a global variable without using it in a function also slows things down (by about the same amount as incrementing a local var). PHP probably does a check to see if the global exists. 仅定义一个局部变量而没在函数中调用它,同样会减慢速度(其程度相当于递增一个局部变量)。PHP大概会检查看是否存在全局变量。

25. Method invocation appears to be independent of the number of methods defined in the class because I added 10 more methods to the test class (before and after the test method) with no change in performance. 方法调用看来与类中定义的方法的数量无关,因为我(在测试方法之前和之后都)添加了10个方法,但性能上没有变化。

26. Methods in derived classes run faster than ones defined in the base class. 派生类中的方法运行起来要快于在基类中定义的同样的方法。

27. A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar++ operations. A similar method call is of course about 15 $localvar++ operations. 调用带有一个参数的空函数,其花费的时间相当于执行7至8次的局部变量递增操作。类似的方法调用所花费的时间接近于15次的局部变量递增操作。

28. Surrounding your string by ‘ instead of " will make things interpret a little faster since php looks for variables inside "…" but not inside ‘…’. Of course you can only do this when you don’t need to have variables in the string. 用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会。当然,只有当你不需要在字符串中包含变量时才可以这么做。

29. When echoing strings it’s faster to separate them by comma instead of dot. Note: This only works with echo, which is a function that can take several strings as arguments. 输出多个字符串时,用逗号代替句点来分隔字符串,速度更快。注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中说echo是语言结构,不是真正的函数,故把函数加上了双引号)。

30. A PHP script will be served at least 2-10 times slower than a static HTML page by Apache. Try to use more static HTML pages and fewer scripts. Apache解析一个PHP脚本的时间要比解析一个静态HTML页面慢2至10倍。尽量多用静态HTML页面,少用脚本。

31. Your PHP scripts are recompiled every time unless the scripts are cached. Install a PHP caching product to typically increase performance by 25-100% by removing compile times. 除非脚本可以缓存,否则每次调用时都会重新编译一次。引入一套PHP缓存机制通常可以提升25%至100%的性能,以免除编译开销。

32. Cache as much as possible. Use memcached - memcached is a high-performance memory object caching system intended to speed up dynamic web applications by alleviating database load. OP code caches are useful so that your script does not have to be compiled on every request. 尽量做缓存,可使用memcached。memcached是一款高性能的内存对象缓存系统,可用来加速动态Web应用程序,减轻数据库负载。对运算码(OP code)的缓存很有用,使得脚本不必为每个请求做重新编译。

33. When working with strings and you need to check that the string is either of a certain length you’d understandably would want to use the strlen() function. This function is pretty quick since it’s operation does not perform any calculation but merely return the already known length of a string available in the zval structure (internal C struct used to store variables in PHP). However because strlen() is a function it is still somewhat slow because the function call requires several operations such as lowercase & hashtable lookup followed by the execution of said function. In some instance you can improve the speed of your code by using an isset() trick. 当操作字符串并需要检验其长度是否满足某种要求时,你想当然地会使用strlen()函数。此函数执行起来相当快,因为它不做任何计算,只返回在zval结构(C的内置数据结构,用于存储PHP变量)中存储的已知字符串长度。但是,由于strlen()是函数,多多少少会有些慢,因为函数调用会经过诸多步骤,如字母小写化(译注:指函数名小写化,PHP不区分函数名大小写)、哈希查找,会跟随被调用的函数一起执行。在某些情况下,你可以使用isset()技巧加速执行你的代码。

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

Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it’s execution does not require function lookups and lowercase. This means you have virtually no overhead on top of the actual code that determines the string’s length. 调用isset()恰巧比strlen()快,因为与后者不同的是,isset()作为一种语言结构,意味着它的执行不需要函数查找和字母小写化。也就是说,实际上在检验字符串长度的顶层代码中你没有花太多开销。

34. When incrementing or decrementing the value of the variable $i++ happens to be a tad slower then ++$i. This is something PHP specific and does not apply to other languages, so don’t go modifying your C or Java code thinking it’ll suddenly become faster, it won’t. ++$i happens to be faster in PHP because instead of 4 opcodes used for $i++ you only need 3. Post incrementation actually causes in the creation of a temporary var that is then incremented. While pre-incrementation increases the original value directly. This is one of the optimization that opcode optimized like Zend’s PHP optimizer. It is still a good idea to keep in mind since not all opcode optimizers perform this optimization and there are plenty of ISPs and servers running without an opcode optimizer. 当执行变量$i的递增或递减时,$i++会比++$i慢一些。这种差异是PHP特有的,并不适用于其他语言,所以请不要修改你的C或Java代码并指望它们能立即变快,没用的。++$i更快是因为它只需要3条指令(opcodes),$i++则需要4条指令。后置递增实际上会产生一个临时变量,这个临时变量随后被递增。而前置递增直接在原值上递增。这是最优化处理的一种,正如Zend的PHP优化器所作的那样。牢记这个优化处理不失为一个好主意,因为并不是所有的指令优化器都会做同样的优化处理,并且存在大量没有装配指令优化器的互联网服务提供商(ISPs)和服务器。

35. Not everything has to be OOP, often it is too much overhead, each method and object call consumes a lot of memory. 并不是事必面向对象(OOP),面向对象往往开销很大,每个方法和对象调用都会消耗很多内存。

36. Do not implement every data structure as a class, arrays are useful, too. 并非要用类实现所有的数据结构,数组也很有用。

37. Don’t split methods too much, think, which code you will really re-use. 不要把方法细分得过多,仔细想想你真正打算重用的是哪些代码?

38. You can always split the code of a method later, when needed. 当你需要时,你总能把代码分解成方法。

39. Make use of the countless predefined functions. 尽量采用大量的PHP内置函数。

40. If you have very time consuming functions in your code, consider writing them as C extensions. 如果在代码中存在大量耗时的函数,你可以考虑用C扩展的方式实现它们。

41. Profile your code. A profiler shows you, which parts of your code consumes how many time. The Xdebug debugger already contains a profiler. Profiling shows you the bottlenecks in overview. 评估检验(profile)你的代码。检验器会告诉你,代码的哪些部分消耗了多少时间。Xdebug调试器包含了检验程序,评估检验总体上可以显示出代码的瓶颈。

42. mod_gzip which is available as an Apache module compresses your data on the fly and can reduce the data to transfer up to 80%. mod_zip可作为Apache模块,用来即时压缩你的数据,并可让数据传输量降低80%。

43. Excellent Article (http://phplens.com/lens/php-book/optimizing-debugging-php.php)about optimizing php by John Lim 另一篇优化PHP的精彩文章,由John Lim撰写。

PHP读取索爱的便签(.vnt格式)

这两天因为公司年会的关系,要上台说两句,要准备点演讲稿什么的。

但是工作又太多,只能抽空在手机(索爱W595c)上用便笺(有的说便签)来起草下。但是用蓝牙发送到电脑上一看,是.vnt格式,用记事本查看一堆字母:

BEGIN:VNOTE
VERSION:1.1
BODY;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=A2=9E=E5=8A=A0;=E4=BB=8E=
=E6=88=91=E4=BB=AC=E6=8A=80=E6=9C=AF=E7=9A=84=E8=A7=92=E5=BA=A6=E6=9D=A5=E7=
=9C=8B,=E6=84=8F=E5=91=B3=E7=9D=80=E6=88=91=E4=BB=AC=E7=9A=84=E7=B3=BB=E7=
=BB=9F=E8=B6=8A=E6=9D=A5=E8=B6=8A=E5=A4=8D=E6=9D=82,=E8=B6=8A=E6=9D=A5=E8=
=B6=8A=E9=9A=BE=E4=BA=8E=E7=BB=B4=E6=8A=A4!=0A=0A=E6=84=9F=E8=B0=A2=E5=90=
=84=E4=BD=8D=E9=A2=86=E5=AF=BC=E5=AF=B9=E4=BA=8E=E6=88=91=E4=BB=AC=E6=88=91=
=E5=B7=A5=E4=BD=9C=E7=9A=84=E5=85=B3=E5=BF=83=E4=B8=8E=E6=8C=87=E5=AF=BC,=
=E5=B8=AE=E5=8A=A9=E6=88=91=E4=BB=AC=E6=8F=90=E9=AB=98;=E6=84=9F=E8=B0=A2=
=E7=BC=96=E8=BE=91=E9=83=A8=E5=90=8C=E4=BA=8B=E5=AF=B9=E4=BA=8E=E6=88=91=E4=
=BB=AC=E4=BA=A7=E5=93=81=E7=9A=84=E6=94=AF=E6=8C=81=E4=B8=8E=E5=8F=8D=E9=A6=
=88;=E5=90=8C=E6=97=B6=E6=84=9F=E8=B0=A2=E6=88=91=E4=BB=AC=E8=BE=9B=E8=8B=
=A6=E5=9C=A8=E4=B8=80=E7=BA=BF=E7=9A=84=E9=94=80=E5=94=AE=E5=90=8C=E4=BB=81=
!=0A=0A=E6=96=B0=E7=9A=84=E4=B8=80=E5=B9=B4,=E6=96=B0=E7=9A=84=E6=9C=BA=E9=
=81=87=E4=B8=8E=E6=8C=91=E6=88=98!=E6=88=91=E4=BB=AC=E6=8A=80=E6=9C=AF=E9=
=83=A8=E5=B0=86=E7=BB=A7=E7=BB=AD=E4=B8=BA=E5=8C=96=E9=BE=99=E5=B7=B7,=E4=
=B8=BA=E5=A4=A7=E5=AE=B6=E8=80=8C=E5=8A=AA=E5=8A=9B!=0A=0A=E8=B0=A2=E8=B0=
=A2!=0A
DCREATED:20100120T101918Z
LAST-MODIFIED:20100121T105654Z
CLASS:PUBLIC
X-IRMC-LUID:000000010013
END:VNOTE

[图]

本来想找找转换工具,没找到,但是找到一点启发:http://wenda.tianya.cn/wenda/thread?tid=418d01d801a7a34d

于是自己用PHP写了个转换工具,还真的成功了!

第一步,复制内容主体:

[图]

第二步,新建PHP文件:

[图]

最后,在浏览器中查看:

[图]

 

在线测试:http://www.veryide.com/products/vnote.reader.php

配置CuteFTP,让它更好用

使用CuteFTP也有不少历史了,也积累了一点使用心得之类的东东,这里就一一整理一下,以备不时之需。

[图]

sm.dat

这个文件是CuteFTP的站点数据库文件,默认情况下位于系统盘(C盘)的用户文件夹中,sm.dat 放在这里风险太高,所以呢,我们要给它移动一下。

将现在用的sm.dat复制到安全的路径存放,再到站点管理器路径中设置一下:

[图]

应用程序助手

通俗点讲,就是配置一下文件的编辑器,这样通过CuteFTP,可以调用指定的编辑器对文件进行编辑。

[图]

语言

这个就不用多说了,设置界面语言的地方:

[图]

 

解决 Dreamweaver CS4 在 Windows7 下的菜单错误

最近用上了Windows7,之前在XP的上 Dreamweaver CS4 启动后,菜单都乱了套,重新安装也不能解决。

[图]

Google 一下,发现有不少人遇到类似情况,不过没有找到解决办法。

http://bbs.iwindows7.com/thread-25361-1-1.html

没办法只有自己找方法了,切换用户,用 Administrator 这个用户重新安装一下,安装成功后运行一下,居然菜单是正常的,我以为这样就OK了,没想到切换到  Lay 这个用户再打开又不行了,还是像上面一样。

[图]

看看这菜单,结构很像是XML嘛,咦,会不会是配置文件出了问题。

Administrator 这个用户的配置文件存放于 C:\Users\Administrator\AppData\Roaming\Adobe\Dreamweaver CS4\zh_CN\Configuration

Lay 这个用户的配置文件存放于 C:\Users\Lay\AppData\Roaming\Adobe\Dreamweaver CS4\zh_CN\Configuration

经过对比发现,Menus这个文件夹中的部分文件内容果然是不一样。

[图]

于是,我把 C:\Users\Administrator\AppData\Roaming\Adobe\Dreamweaver CS4\zh_CN\Configuration\Menus 中的内容全部复制到 C:\Users\Lay\AppData\Roaming\Adobe\Dreamweaver CS4\zh_CN\Configuration\Menus 中,再用 Lay 用户登录 Windows7 ,打开 Dreamweaver CS4 果然OK了。

[图]

PS.如果一直只使用 Administrator 这个用户的话,应该不会遇到类似问题。

VeryIDE Apps 整合 UCenter 指南

此文档针对 VeryIDE Apps 商业版用户,整合康盛 UCenter API,可以实现论坛/SNS等产品同步登录/退出。

首先登录 UCenter 后台管理面板,在“应用管理”中“添加新应用”:

[图]

[图]

添加成功后,再次进入“编辑”页面,将以下“配置信息”复制到剪贴板。

[图]

打开“veryideextendsconfig.inc.php”配置文件,将上面复制的代码粘贴到此文件。

[图]

[图]

这时返回 UCenter ,应该可以看到“通信成功”字样。

[图]

再返回到表单编辑,勾选“注册用户提交”就能实现 Discuz 用户登录验证了。

[图]

如果发现不能与 Disucz 同步登录/退出 请按下面进行配置。

[图]

VeryIDE Apps 2.0 Bug 排除手册

  1. 更新广告失效?
    在更新管理中执行如下语句,再对广告进行修改:
    ALTER TABLE `{TableModPre}ads` ADD `a_area` VARCHAR( 200 ) NOT NULL AFTER `a_share` ;

  2. 危险程序扫描中报告 veryide/cache/system/system.menu.php 存在风险,请到菜单管理页点击“保存更改”刷新下菜单缓存即可

  3. 更新管理中安装1.5.8版后,再安装2.0版失败的问题:
    虽然提示安装2.0版失败,其实SQL语句已经成功执行,所以数据库已经有更新,可以正常使用。

  4. 模块管理页,安装更新后部分模块都显示“禁用”状态,这个是由于错误打包了本地的缓存的缘故,你可以手动启用所有模块即可解决。

  5. 广告分类不对了?
    请到“广告位置”点击右侧的“更新缓存”进行一下更新就OK了。

  6. 广告统计数据不对?
    目前数据都乘以了10,也就是20会显示200,修改此倍数请到 veryide/modules/ads.config.php
    请将 $cfg_ads_multiple=10; 改为 $cfg_ads_multiple=1;

VeryIDE Apps 产品迁移指南

在使用 VeryIDE Apps 的过程中,如遇到服务器迁移或环境变化等原因,需要对 VeryIDE 进行重新配置的话,请按照以下步骤进行操作。

修改配置文件,位于:veryide/common/veryide.config.php

[图]

VI_ROOT VeryIDE 所在目录地址,相对于主目录的地址。
例如,您的VeryIDE Apps访问地址是:http://localhost/apps/ 那么此处的值就是:/apps/

VI_START 初次安装日期时间,是Unix时间戳,可以不用设置

VI_DBHOST 数据库服务器地址,本地服务器是 localhost 否则应为一个IP地址

VI_DBPORT 数据库连接端口,默认为 3306

VI_DBNAME 数据库的名称

VI_DBUSER 登录数据库使用的用户名

VI_DBPASS 登录数据库使用的用户密码

 

VeryIDE Apps 2.0 新特性介绍

VeryIDE Apps 2.0 经过两个月左右开发,终于要和大家见面了!

此次升级将会给大家焕然一新的感觉,包括全新的操作界面和向导式“气泡”提示。

[图]

下面就较大变化的改进作简短介绍,更多的功能和特性还是要自己亲自用用才能发现哦。

实用高效的站内搜索引擎

[图]

方便快捷的快速入口

[图]

针对小屏幕的“侧边栏”控制

[图]

[图]

“侧边栏”隐藏后会出现辅助的快捷菜单

[图]

危险代码扫描程序

[图]

方便升级调试的在线SQL语句执行工具

[图]

系统优化和清理工具

[图]

导航控制按钮更加方便浏览

[图]

广告系统特别增强:广告定向地区投放

[图]

[公告] VeryIDE Apps 免费版更新放缓

首先感谢大家一直以来对 VeryIDE Apps 的关心与支持,不论是免费用户还是商业授权用户,谢谢大家。

由于我们人力与财力有限,免费版的更新与支持占据了我们大部分精力,已经严重影响到了产品的开发与完善。

经过慎重考虑,我们决定暂时放缓对 VeryIDE Apps 免费版的更新服务,但不排除以后会保持与商业版同步的更新服务。

对于 VeryIDE Apps 免费版中发现的严重程序BUG或是影响程序正常运行的问题,我们将会提前以补丁的形式发布更新,但是核心程序与功能放缓升级。

任何产品的成功都离不开资金的支持,如果您支持并考虑继续使用 VeryIDE Apps ,可以向我们购买商业版。

VeryIDE Apps 免费版会继续更新,请放心,对于免费用户我们也不会轻易放弃。

VeryIDE Apps 商业版功能更强,更好的扩展性,将为您提供更好的应用。

如果商业版中有您需要的功能或特性,我们推荐您购买商业版。

谢谢广大用户一直的关心与支持!

 

VeryIDE Apps 开发小组

VeryIDE Apps 使用手册:表单数据导出

在表单中收集的用户数据可以使用导出工具进行导出,格式为 CSV,可以使用 Excel 打开。

[图]

在这里有一个地方需要注意,在“电话号码”和“QQ/MSN”两项数据前都有一个单引号,这是为了解决 Excel 的一个特性:在 Excel 单元格中键入长数字时,最后一些位数被更改为零

[图]

选中需要转换的列,选择菜单中的“数据”-> “分列”:

[图]

在“文本分列向导”中按下面的流程操作:

[图]

[图]

[图]

点“完成”之后就能看到转换完成后的数据了:

[图]

编辑完成后,需要点“保存”,在弹出的对话框中选“否”,将数据“保存”为 xls 格式。

[图]

.xls 格式的数据可以直接使用 Excel打开。