Home Backend Development PHP Tutorial PHP代码优化技巧小结_PHP

PHP代码优化技巧小结_PHP

May 30, 2016 am 08:44 AM
php code optimization

PHP优化的目的是花最少的代价换来最快的运行速度与最容易维护的代码。给大家提供下面这些优化技巧:

1. echo 比 print 快。

2. 使用echo的多重参数代替字符串连接。

3. 在执行for循环之前确定最大循环数,不要每循环一次都计算最大值,最好运用foreach代替。

4. 对global变量,应该用完就unset()掉。

5. 用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会。

6. 函数代替正则表达式完成相同功能。

7. 当执行变量$i的递增或递减时,$i++会比++$i慢一些。这种差异是PHP特有的,并不适用于其他语言,++$i更快是因为它只需要3条指令(opcodes),$i++则需要4条指令。后置递增实际上会产生一个临时变量,这个临时变量随后被递增。而前置递增直接在原值上递增。

8. 使用选择分支语句(switch case)好于使用多个if,else if语句。

9. 利用var_dump进行PHP代码调试。如果你在寻找php调试技术,我必须说var_dump应该是你要找的目标,在显示php信息方面这个命令可以满足你的所有需要,而调试代码的多数情况与得到PHP中的数值有关。

10. 在包含文件时使用完整路径,解析操作系统路径所需的时间会更少。

11. 动辄创建全局数值是一种糟糕的做法,不过有时候实际情况的确又需要这么做。对于数据库表或数据库连接信息使用全局数值是一个不错的想法,但不要在你的PHP代码中频繁使用全局数值。另外,更好的一种做法是把你的全局变量存放在一个config.php文件中。

12. 如果你想知道脚本开始执行的时刻,使用$_SERVER[‘REQUEST_TIME']要好于time()。

13. 打开apache的mod_deflate模块。

14. 用@屏蔽错误消息的做法非常低效。

15. 尽量采用大量的PHP内置函数。

16. 递增一个未预定义的局部变量要比递增一个预定义的局部变量慢9至10倍。

17. 派生类中的方法运行起来要快于在基类中定义的同样的方法。

18. 仅定义一个局部变量而没在函数中调用它,同样会减慢速度(其程度相当于递增一个局部变量)

19. Apache解析一个PHP脚本的时间要比解析一个静态HTML页面慢2至10倍。尽量多用静态HTML页面,少用脚本。

20. 正如之前提到的,任何php网站中最重要的部分有99%的可能是数据库。因此,你需要非常熟悉如何正确的使用sql,学会关联表和更多高级的数据库技术。

21. 调用带有一个参数的空函数,其花费的时间相当于执行7至8次的局部变量递增操作。

22. 当操作字符串并需要检验其长度是否满足某种要求时,你想当然地会使用strlen()函数。此函数执行起来相当快,因为它不做任何计算,只返回zval 结构(C的内置数据结构,用于存储PHP变量)中存储的已知字符串长度。

23. 并不是所有情况都必须使用面向对象开发,面向对象往往开销很大,每个方法和对象调用都会消耗很多内存。

24. 除非脚本可以缓存,否则每次调用时都会重新编译一次。引入一套PHP缓存机制通常可以提升25%至100%的性能,以免除编译开销。

最后再提醒一下在代码优化的时候应该注意的事情:

1. 短代码不等于快的代码
很多人在写程序时希望将代码写的越简洁越好,但是越短的代码有时候反而需要更长的执行时间。

2. 在写程序的时候更应该注重程序的扩展性,而不是追求速度。

3. 在优化你的代码之前,先看看跟数据库有关的部分,因为大多数应用程序的瓶颈在数据库而不是代码。

4. 微优化得不偿失
什么叫做微优化?就像前面所说的将正则表达式部分的代码改用字符串函数代替。这样做有以下缺点:
(1)花费时间较长
(2)不会根本上解决性能问题
(3)很有可能会破坏以前的代码从而产生未知的错误
(4)付出大于回报

       这里还不得不提到一个误区,有些人为了让程序更加优化,在分析业务逻辑的时候便将优化考虑在内了,从而为了得到更优的代码而改动业务逻辑。这是十分愚蠢的想法,因为程序的目的便是为了处理现实中遇到的问题,我们不能本末倒置。

以上就是php代码优化的方法和一些小提示,希望能够帮助到大家更好的优化php代码

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles