PHP代码优化技巧小结_php技巧
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代码

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











As one of the most popular programming languages in the world, Java has become the language of choice for many businesses and developers. However, code refactoring is crucial to maintaining code quality and development efficiency. Java code can become increasingly difficult to maintain over time due to its complexity. This article will discuss how to refactor Java code to improve code quality and maintainability. Understand the principles of refactoring The purpose of Java code refactoring is to improve the structure, readability and maintainability of the code, rather than simply "changing the code". because

Program performance optimization methods include: Algorithm optimization: Choose an algorithm with lower time complexity and reduce loops and conditional statements. Data structure selection: Select appropriate data structures based on data access patterns, such as lookup trees and hash tables. Memory optimization: avoid creating unnecessary objects, release memory that is no longer used, and use memory pool technology. Thread optimization: identify tasks that can be parallelized and optimize the thread synchronization mechanism. Database optimization: Create indexes to speed up data retrieval, optimize query statements, and use cache or NoSQL databases to improve performance.

Code optimization techniques in PHP high concurrency processing With the rapid development of the Internet, high concurrency processing has become an important issue in web application development. In PHP development, how to optimize code to cope with high concurrent requests has become a difficult problem that programmers need to solve. This article will introduce some code optimization techniques in PHP high concurrency processing, and add code examples to illustrate. Reasonable use of cache For high concurrency situations, frequent access to the database will lead to excessive system load and relatively slow access to the database. Therefore, we can

In Java framework performance optimization, code optimization is crucial, including: 1. Reduce object creation; 2. Use appropriate data structures; 3. Avoid blocking I/O; 4. Optimize string operations; 5. Avoid reflection. By following these tips, you can improve framework performance, such as optimizing Hibernate queries to reduce the number of database calls.

1. Code optimization to avoid using too many security annotations: In Controller and Service, try to reduce the use of @PreAuthorize and @PostAuthorize and other annotations. These annotations will increase the execution time of the code. Optimize query statements: When using springDataJPA, optimizing query statements can reduce database query time, thereby improving system performance. Caching security information: Caching some commonly used security information can reduce the number of database accesses and improve the system's response speed. 2. Use indexes for database optimization: Creating indexes on tables that are frequently queried can significantly improve the query speed of the database. Clean logs and temporary tables regularly: Clean logs and temporary tables regularly

With the continuous deepening of software development and the continuous accumulation of code, code refactoring has become an inevitable part of the modern software development process. It is a process of modifying the established code of a system to improve its structure, performance, readability, or other related aspects. In this article, we will explore how to do code refactoring in Go language. Defining Refactoring Goals Before starting code refactoring, we should set a clear refactoring goal. We need to ask ourselves some questions, such as what are the problems with this code? We need to reconstruct

C++ is a very powerful, flexible and widely used programming language. However, with the continuous development of projects and the continued relative reuse of code, there will be problems such as a decrease in code quality and readability. At this time, the code needs to be refactored to achieve better code quality and higher maintainability. This article will introduce how to refactor C++ code. Define your goals Before you start refactoring your code, you need to define what you want to accomplish. For example, you may want to improve code readability, reduce code duplication, improve code performance, etc. none

In actual development, in order to achieve better performance and higher scalability of a website or application, the optimization of PHP code is a very important step. Here are some PHP performance tips to help your code run faster. 1. Minimize function calls and variables 1.1 Function calls Function calls have a great impact on the performance of PHP code, because each function needs to allocate space in memory. When writing PHP code, you should try to avoid too many function calls and use inline functions or custom functions instead. 1.2 Variables
