优化PHP代码的四十三条建议(推荐)_PHP教程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了优化PHP代码的四十三条建议(推荐)_PHP教程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2930字,纯文字阅读大概需要5分钟。
内容图文
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()函数。
- 共2页:
- 上一页
- 1
- 2
- 下一页
http://www.bkjia.com/PHPjc/364246.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/364246.htmlTechArticle1. If a method can be static, declare it static. Speed improvement is by a factor of 4. 如果一个方法可静态化,就对它做静态声明。速率可提升至4倍。 2. ech...
内容总结
以上是互联网集市为您收集整理的优化PHP代码的四十三条建议(推荐)_PHP教程全部内容,希望文章能够帮你解决优化PHP代码的四十三条建议(推荐)_PHP教程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。