(转载)优化PHP代码的40条建议
原文链接:http://webdev.csdn.net/page/58438f5e-704e-4e80-bdbd-7d06e8ecceb9#
40 Tips for optimizing your php Code
原文地址: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 redund
相关文档:
要使您的FCKeditor能够使用上传功能,您必须进行以下配制。
注意:FCKeditor不支持虚拟目录,您的路径设置都是针对网站根目录的绝对路径而言的。这点对于发布到远程网站目录的开发者极为不便,后面我们会对此进行讨论。
一、打开fckeditor\editor\filemanager\upload\php\config.php,找到代码$Config['Enabled'],将值 ......
以下是windows环境的配置:
一、进入apache的bin目录
运行
set OPENSSL_CONF= openssl.cnf
openssl req
-new -nodes -keyout server.key -out server.csr
然后出现:
Country Name (2 letter
code):使用国际标准组织(ISO)国码格式,填写2个字母的国家代号。中国请填写CN。
State or Provi ......
memcache个人理解主要是用来管理内存的,php和memcache结合就可以使php吧一些数据保存在memcache管理的内存里,方便管理,也快,还支持多台服务器共享数据,废话不多说. 分为linux和windows 1)linux和类Unix可以从以下地址下载memcached和libevent,由于安装memcache需要libevent支持,所以需要先安装libevent memcached官方 ......
前言
正如大家一样,当我觉得要学习一种新的东西的时候,总是有一种巨大的压力.首先一个问题是:是否可以学会.另一个问题则是:这个新的知识是否能够找到一份工作.如果你还是一名学生,那么你应该感到高兴,因为学校里的生活不是由你自己提供保障的,所以不要考虑温保问题;但是自从学校出来后,才知道生活是什么滋味 ......
PHP关于时间日期的处理不是很规范,简单就简单了,就是不知道输入的字符串是否能够正确转化为需要的DateTime类型。
面向对象的PHP应该使用DateTime类来做string和dateTime的转换
从字符串到时间类型
DateTime::createfromFormat(‘m/d/Y H:i','03/01/2008 02:20');
$totalPrice=0.0;
从DateTime到字符串
$ ......