最佳化PHP代碼的四十三條建議(推薦)_PHP教程

來源:互聯網
上載者:User
  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函數代替Regex完成相同功能。

  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...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.