由 徐永久 发表于 2001年10月16日 08:57。
本文对 PHP的优化,缓冲,压缩 提出了实际的解决方案
作为流行的 Web 编程语言, PHP 的最大优势就是速度。 PHP4 已经在这方面做的非常好了,你几乎找不到比它更快的脚本编程语言了。但是如果你的应用负荷很大,而带宽又比较小,或者有其他的瓶颈影响你的服务器性能,那么,你不妨试试笔者为你开出的几个药方,看看是否灵验。
一、代码优化
一谈到代码优化,或许你想到的就是整齐明了的代码,但是本文的意思却不是在此,因为如果要寻求速度的话,就要对PHP 源码作相应的调整。一般说来就是去掉多余的注释,让代码不可读。但是这对于一个具有良好素养的程序员来说,简直就是不可思议的。好在Zend Technologies 公司发布了 Zend 优化引擎可以帮助你做到这一点。它现在是免费的,但是你必须遵循 Zend Optimizer 许可。这个产品可以对引擎产生的中间代码进行优化。
安装这个引擎比较简单,下载对应平台的版本以后,解开压缩文件,然后在 php.ini 文件里面加上下面两行,重新启动 Web 服务器,就搞定了。
zend_optimizer.optimization_level=15
zend_extension="/path/to/ZendOptimizer.so"
zend_loader.enable=Off
如果是 Win32 平台的话就应该是:
zend_optimizer.optimization_level=15
zend_extension_ts="C:\path\to\ZendOptimizer.dll"
zend_loader.enable=Off
啊!没有搞错吧?怎么是三行?其实第三行是可选的。因为看起来把 zend_loader 关掉能提高一点速度,因此值得把这第三行放到 php.ini 。需要注意的是,关掉的前提条件是你没有在使用 Zend 加密程序。
二、缓冲
如果想要更进一步提升速度,我们就需要考虑采用缓冲技术了。有一些可选的解决方案,包括 Zend Cache (测试版本), APC, 以及 Afterburner Cache,另外还有 jpCache 等。
以上这些都是属于缓冲模块,他们把第一次对 .php 文件请求产生的中间代码存储在 Web 服务器的内存中,然后对以后的请求返回“编译好”的版本。因为这样减少了磁盘读写,而且都在内存工作,所以这个过程能显著提升应用性能,
现成的这类产品比较多,到底选择谁呢?
Zend Cache 是一款不错的商业产品,在第一次加载那些很大的 PHP 页面后,你会明显感受到速度的提升,服务器会留出更多的资源。可惜这个产品是要花银子的,但是在有些情形下,你可不要吝啬这些银子。
Afterburner Cache 是 Bware Technologies 的产品,目前还处于 Beta 版本,看起来似乎和 Zend Cashe 一样,但是它不能达到 Zend Cache 那样好的效果,也不能和 Zend 优化引擎一起工作,但是它是免费的,所以我采用了这个模块。
APC (Alternative PHP Cache) 是 Community Connect 发布的又一个免费模块,看起来似乎可以用于生产环境了。
三、Web 内容压缩
对于日益拥挤的网络来说,节约带宽就像节约用水一样是十分值得提倡的。根据IETF 标准,大多数浏览器应该支持使用 gzip 压缩的内容。也就是说你可以把用 gzip 压缩的内容发送给浏览器,浏览器会透明的解压数据。
mod_gzip 是 Remote Communications 公司推出的免费 Apache 模块,能把静态的Web 内容压缩后发送给浏览器。对于大多数静态网页来说,这个模块十分合适。尽管
Remotecommunications 公司的人说这个模块支持所有那些 mod_php, mod_perl,mod 什么产生的动态内容,但是看起来还是不能工作,从 mod_gzip 的邮件列表来看,这个问题估计要到1.3.14.6f 才能解决。
如果要压缩动态内容的话,我们可以采用class.gzip_encode.php,一个在脚本开始和结束时使用的 PHP 类。对整个网站来说就是在 php.ini 的 auto_prepend 和 auto_append 中调用其中的函数。详细你可以阅读这个类的程序,这个程序注释得很好,作者几乎把什么都告诉你了。不过使用之前,你的 PHP 要编译为支持 zlib。
对于 PHP 4.0.4 来说,一个新的解决方案就是使用 ob_gzhandler,能达到和上面的类一样的效果,只要简单的在 php.ini 加入下面这句话就可以了:
output_handler = ob_gzhandler ;
这能让 PHP 激活输出缓冲,并压缩所有输出。如果有什么特殊的理由不想让所有的内容都压缩输出的话,可以采用在 .htaccess 文件中加入下面的行,对对应目录下的文件进行压缩。
php_value output_handler ob_gzhandler
也可以直接在 PHP 代码中加入:
ob_start("ob_gzhandler");
这项压缩技术十分有效,但是对 Netscape Communicator 用户来说,因为不能压缩图形文件,所以看上去没有完姆⑺停虼吮匦牍乇斩?jpeg 和 gif 文件的压缩,IE 没有这个问题。
结论:
采用本文所讨论的技术应该能改善你的网站性能,但是需要注意的是:
- PHP 可能不是导致瓶颈的原因,仔细检查其他原因(例如:数据库)
- 你不可能把服务器性能调节到最高状态。因此在埋怨 PHP 及其缓冲之前,考虑是否该升级服务器了,或者采用动态负载平衡技术(那可是一大笔银子哦)。
- 不要低估内容压缩,在你 100 Mb 的内部网上面看到 PHP 应用的速度提升时,不要忘记使用调制解调器的用户在哪里埋怨你的 100Kb 的 HTML 页面。

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

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