0、用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作
参数的“函数”(译注:PHP手册中说echo是语言结构,不是真正的函数,故把函数加上了双引号)。
1、如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍。
2、$row[’id’] 的速度是$row[id]的7倍。
3、echo 比 print 快,并且使用echo的多重参数(译注:指用逗号而不是句点)代替字符串连接,比如echo $str1,$str2。
4、在执行for循环之前确定最大循环数,不要每循环一次都计算最大值,最好运用foreach代替。
5、注销那些不用的变量尤其是大数组,以便释放内存。
6、尽量避免使用__get,__set,__autoload。
7、require_once()代价昂贵。
8、include文件时尽量使用绝对路径,因为它避免了PHP去include_path里查找文件的速度,解析操作系统路径所需的时间会更少。
9、如果你想知道脚本开始执行(译注:即服务器端收到客户端请求)的时刻,使用$_SERVER['REQUEST_TIME'] 要好于time()
10、函数代替正则表达式完成相同功能。
11、str_replace函数比preg_replace函数快,但strtr函数的效率是str_replace函数的四倍。
zz
12、如果一个字符串替换函数,可接受数组或字符作为参数,并且参数长度不太长,那么可以考虑额外写一段替换代码,使得每次传递参数是一个字符,而不是只写一行代码接受数组作
为查询和替换的参数。
13、使用选择分支语句(译注:即switch case)好于使用多个if,else if语句。
14、用@屏蔽错误消息的做法非常低效,极其低效。
15、打开apache的mod_deflate模块,可以提高网页的浏览速度。
16、数据库连接当使用完毕时应关掉,不要用长连接。
17、错误消息代价昂贵。
18、在方法中递增局部变量,速度是最快的。几乎与在函数中调用局部变量的速度相当。
19、递增一个全局变量要比递增一个局部变量慢2倍。
20、递增一个对象属性(如:$this->prop++)要比递增一个局部变量慢3倍。
21、递增一个未预定义的局部变量要比递增一个预定义的局部变量慢9至10倍。
22、仅定义一个局部变量而没在函数中调用它,同样会减慢速度(其程度相当于递增一个局部变量)。PHP大概会检查看是否存在全局变量。
23、方法调用看来与类中定义的方法的数量无关,因为我(在测试方法之前和之后都)添加了10个方法,但性能上没有变化。
24、派生类中的方法运行起来要快于在基类中定义的同样的方法。
25、调用带有一个参数的空函数,其花费的时间相当于执行7至8次的局部变量递增操作。类似的方法调用所花费的时间接近于15次的局部变量递增操作。
26、Apache解析一个PHP脚本的时间要比解析一个静态HTML页面慢2至10倍。尽量多用静态HTML页面,少用脚本。
27、除非脚本可以缓存,否则每次调用时都会重新编译一次。引入一套PHP缓存机制通常可以提升25%至100%的性能,以免除编译开销。
28、尽量做缓存,可使用memcached。memcached是一款高性能的内存对象缓存系统,可用来加速动态Web应用程序,减轻数据库负载。对运算码 (OP code)的缓存很有用,使得
脚本不必为每个请求做重新编译。
29、当操作字符串并需要检验其长度是否满足某种要求时,你想当然地会使用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”$$ }
Calling isset() happens to be faster than strlen() because, unlike the latter, isset(), as a language construct, means that its execution does not require function lookups and lowercase letters. That is, actually in the top-level code that checks the string length you
Not much expense.
30. When executing the increment or decrement of variable $i, $i++ will be slower than ++$i. This difference is specific to PHP and does not apply to other languages, so please don't modify your C or Java code and expect it to be instantly faster, it won't work. ++$i
It is faster because it only requires 3 instructions (opcodes), while $i++ requires 4 instructions. Post-increment actually creates a temporary variable that is subsequently incremented. Prefix increment increases directly on the original value. This is a type of optimization processing.
Just like Zend’s PHP optimizer does. It's a good idea to keep this optimization in mind, as not all command optimizers do the same optimizations, and there are a large number of ISPs that don't have command optimizers installed.
(ISPs) and servers.
31. Not everything must be object-oriented (OOP), object-oriented is often very expensive, and each method and object call consumes a lot of memory.
32. It is not necessary to use classes to implement all data structures, arrays are also very useful.
33. Don’t subdivide the methods too much. Think carefully about which code you really intend to reuse?
34. You can always break the code into methods when you need it.
35. Try to use a large number of PHP built-in functions.
36. If there are a large number of time-consuming functions in the code, you can consider implementing them using C extensions.
37. Profile your code. The checker will tell you which parts of the code take how much time. The Xdebug debugger includes inspection programs that evaluate the overall integrity of your code and reveal bottlenecks in your code.
38. mod_zip can be used as an Apache module to instantly compress your data and reduce data transmission volume by 80%.
39. When file_get_contents can be used instead of file, fopen, feof, fgets and other series of methods, try to use file_get_contents because it is much more efficient! But be careful when opening a file_get_contents
PHP version problem when URL file;
40. Perform as few file operations as possible, although PHP’s file operation efficiency is not low;
41. Optimize the Select SQL statement and perform as few Insert and Update operations as possible (I have been criticized for updating);
42. Use PHP internal functions as much as possible (but in order to find a function that does not exist in PHP, I wasted time that could have been written by writing a custom function. It is a matter of experience!);
43. Do not declare variables inside the loop, especially large variables: objects (this seems to be not just a problem in PHP?);
44. Try not to loop and nest assignments in multi-dimensional arrays;
45. Do not use regular expressions when you can use PHP’s internal string manipulation functions;
46. foreach is more efficient. Try to use foreach instead of while and for loops;
47. Use single quotes instead of double quotes to quote strings;
48. "Replace i=i+1 with i+=1. It conforms to the habits of c/c++ and is more efficient";
49. Global variables should be unset()ed when used;
Reference: 50 rules to improve PHP development and efficiency tips