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()函數。