Home  >  Article  >  Backend Development  >  PHP优化总结

PHP优化总结

WBOY
WBOYOriginal
2016-06-23 14:32:041073browse

1、如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍。
2、include文件时尽量使用绝对路径,因为它避免了PHP去include_path里查找文件的速度。
3、preg_replace 比str_replace速度快,strtr的执行速度将近是preg_replace的4倍。
4、$row[’id’] 的速度是$row[id]的7倍。
5、用foreach代替for ($x=0; $x 6、使用echo的多重参数(指用逗号而不是句点)代替字符串连接。比如echo $str1,$str2
7、使用选择分支语句(译注:即switch case)好于使用多个if,else if语句。
8、用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会。
9、不要为了OO而OO,能用数组代替对象最好。
10、方法调用看来与类中定义的方法的数量无关,因为我(在测试方法之前和之后都)添加了10个方法,但性能上没有变化。所以类文件的大小不会影响性能,当你需要时,你总能把代码分解成方法。

最好让这些总结成为你编码时候的习惯。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:十天学会phpNext article:PHP serialize & JSON 解析