Home Backend Development PHP Tutorial A complete guide to improving PHP execution speed_PHP tutorial

A complete guide to improving PHP execution speed_PHP tutorial

Jul 21, 2016 pm 04:08 PM
and error logging php Complete strategy implement promote set up speed question


Php setup issues & acceleration suggestions If you cannot use the application due to incorrect php settings during use, please check the following parameter settings in php.ini.
The following assumes that your PHP is installed in d:/php/

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Error handling and logging ;

; error_reporting is a bit -field. Or each number up to get desired error
; reporting level
; E_ALL - All errors and warnings
; E_ERROR - fatal run-time errors
; E_WARNING - run-time warnings (non -fatal errors)
; E_PARSE - compile-time parse errors
; E_NOTICE - run-time notices (these are warnings which often result
; from a bug in your code, but it's possible that it was
; intentional (e.g., using an uninitialized variable and
; relying on the fact it's automatically initialized to an
; empty string)
; E_CORE_ERROR - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING - warnings (non-fatal errors) that occur during PHP's
; initial startup
; E_COMPILE_ERROR - fatal compile-time errors
; E_USER_ERROR - user-generated error message
; E_USER_WARNING - user-generated warning message
; E_USER_NOTICE - user-generated notice message

; Examples:

; - Show all errors, except for notices

;error_reporting = E_ALL & ~E_NOTICE

; - Show only errors

;error_reporting = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR

; - Show all errors except for notices

error_reporting = E_ALL & ~E_NOTICE
; Print out errors (as a part of the output). For production web sites,
; you're strongly encouraged to turn this feature off, and use error logging
; instead (see below). Keeping display_errors enabled on a production web site
; may reveal security information to end users, such as file paths on your Web
; server, your database schema or other information.
display_errors = On

; You should do your best to write your scripts so that they do not require
; register_globals to be on; Using form variables as globals can easily lead
; to possible security problems, if the code is not very well thought of.
register_globals = On

; Argument passed to save_handler. In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP's session functions.
session.save_path = "c:/winnt/temp"(can be changed to you existing directory)

; cgi.force_redirect is necessary to provide security running PHP as a CGI under
; most web servers. Left undefined, PHP turns this on by default. You can
; turn it off here AT YOUR OWN RISK
; **You CAN safely turn this off for IIS, in fact, you MUST.**
cgi.force_redirect = 0

; Directory in which the loadable extensions (modules) reside.
extension_dir = ./extensions/
; or set it directly to your absolute directory, such as: d:/php/extensions/

; GD library, for example The picture management system will use it.
; This file is under d:/php/extensions/.
extension=php_gd.dll
One of the advantages of PHP is that it is very fast, which is sufficient for general website applications. However, if the site's traffic is high, bandwidth is narrow, or other factors cause performance bottlenecks on the server, you may have to think of other ways to further increase the speed of PHP. This article will introduce how to do this from several aspects, so as to make users browse more "cool".

Code Optimization


I don’t want to tell you again
how to write cleaner code. I think everyone knows this. When you need speed, you You may have done a lot of work on optimizing PHP source code. What is proposed here is that this tedious work can be completed by other tools. This is Zend Optimizer, a program available for free from Zend Technologies' website (http://www.zend.com/). Its principle is simple, by detecting the intermediate code generated by the Zend engine and optimizing it to obtain higher execution speed. I think optimizing code is a rather tedious task, and the optimized code may become difficult to understand, especially when you put down the PHP program for a while and suddenly the customer asks you to make some changes, you may not know it yourself. Got it ;-). Therefore, I recommend that you use Zend Optimizer to do this optimization work when the PHP source code is relatively complex. The advantage is that it will not make your code complicated and difficult to understand.

Installing Zend Optimizer is very simple. Just download the relevant precompiled libraries according to the platform you are using, add two lines to your php.ini, and restart your web server!

zend_optimizer.optimization_level=15zend_extension="/path/to/ZendOptimizer.so" zend_loader.enable=Off

You may be a little strange, didn't it say two lines, why did it become three lines? . However, the third line is optional. It seems that disabling this zend_loader will make the optimization faster, so you might as well add this line to your php.ini file. It should be noted that zend_loader can be disabled only when you are not using Zend Encoder Runtime. Zend Encoder Runtime will be mentioned below.

Want it faster? Use cache

If your PHP application still needs faster speed, the next solution is caching. There are a few different ways to accomplish this. I have tried Zend Cache (evaluation version), APC and Afterburner Cache myself.

The above mentioned are all "buffer modules".Their principles are similar. When the PHP file is requested for the first time, by storing the intermediate code of your PHP source code in the memory of the web server, for the same request in the future, the "compiled" version in the memory is directly provided. Since it can minimize disk access, this method can indeed greatly improve the performance of PHP. What's more convenient is that when your PHP source code is modified, the buffered module can detect these changes and reload the same, so you don't have to worry about customers getting an old version of the program. These buffered modules are great, but which one should I choose? Let’s introduce them respectively:

Zend Cache is a commercial product of Zend Technologies (it is also the company that provides us with PHP engine and Zend Optimizer for free). It's really not bad. After the first run, you can obviously notice that the speed of PHP has been greatly improved, and the server has more free resources. The downside is that you have to pay for it, but in terms of value for money, it's still well worth it.

Afterburner Cache is a free buffer module provided by Bware Technologies (bwcache.bware.it). Currently only a beta version, it seems to do the same job as Zend Cache, but the performance improvement is not as good as Zend Cache, and the existing version does not work with Zend Optimizer, but it is free.

APC (Alternative PHP Cache) is another free module provided by Community Connect (apc.communityconnect.com). Its work is very stable, and the speed has been greatly improved. It should be noted that I have not found an official test data. These are just tests on my application, so I cannot draw a conclusion. Compression of Web content (making it more "enjoyable" for your customers to use)

After the above two methods, I believe that the performance of your PHP application has been greatly improved. Now it is time to look at it from another aspect Considered: Download speed. If your application is only running within the company, and all customers use 100Mb/s Ethernet to connect to the server, this may not be a problem, but if some of your customers use slow modem connections, you need to consider Use content compression method.
According to IETF specifications, most browsers support gzip content
compression. This means that you can use gzip to compress the web content before sending it to the client's browser. The browser will automatically decompress the data when receiving it and allow the user to see the original page. Likewise, there are several different ways to compress the content of a web page.

mod_gzip is an Apache module provided free of charge by Remote Communications (http://www.phpbuilder.com/columns/www.remotecommunications.com), which can compress static web pages. It works just fine, you just need to compile it with apache (or use it as a DSO). People from Remotecommunications said that it can also compress dynamic content, including mod_php, mod_perl, etc. But I tried it, and it doesn't seem to work. I read on the mod_gzip mailing list that this bug will be fixed in the next version (I think it will be version 1.3.14.6f). But you can still use it for static content compression.

But we also want to compress dynamic content, so we have to find another way. One way is to use class.gzip encode.php (http://leknor.com/code/). Just call this PHP class at the beginning and end of your PHP script to compress the content of your page. If your entire site requires such compression, you can call these functions in auto_prepend and auto_append in your php.ini file. It works pretty well, but on heavily loaded sites it obviously comes with a bit of overhead. To learn more about how it works, take a look at its class code (you'll need to at least compile PHP with zlib support). The author's instructions inside are also very detailed, so you can get everything you need to know.

Recently, I also saw an article about PHP output buffering. What it says is that PHP4.0.4 has introduced a new output buffer processing method-ob_gzhandler. Its function is the same as the class introduced above, but the difference is that you only need to use the following syntax in your php.ini. :

output_handler = ob_gzhandler ;

This will activate PHP's output buffering feature and compress everything it sends. For some special reasons, if you don't want to set it here and only change the default setting where needed (no compression), just modify the .htaccess file in the PHP source code directory that needs to be compressed. The syntax used is as follows:

php_value output_handler ob_gzhandler

...or call it directly in your PHP code, in the following way:

ob_start("ob_gzhandler" );

This method of output buffering is good and does not bring additional system overhead to the server. I highly recommend you use this method. Its change can be illustrated by the following example. If the customer is using a 28.8K modem, after this process, he will think that he has suddenly switched to an ISDN access. One thing to note is: Netscape Communicator does not support image compression, so it will not be displayed.So unless your customers all use Internet Explorer, you must disable compression of jpeg and gif images. There should be no problem with the compression of other files, but I suggest you test it, especially if the browser uses uncommon plug-ins or is a browser that is rarely used.

Other useful stuff...

Zend Technologies’ online store was launched on January 24 this year and sells some interesting PHP-related products. Including the aforementioned Zend Cache, Zend Encoder (simply put, it is a compiler for PHP code that can generate compiled classes so that you can sell to customers without worrying about leaking source code. In the web server that needs to run these classes (will use Zend Encoder Runtime to decode), Zend Ide (an integrated development environment for PHP with many powerful features), and support services for PHP developers.

Conclusion

Using the techniques mentioned in this article, you will be able to greatly improve the performance of your site, but please note the following points:

1. The bottleneck may not be there For PHP, you need to examine every object in the application (such as a database)

2. The performance of a web server is limited, so don’t think that poor performance is due to PHP, it may also be due to access The volume is large, your server needs to be upgraded, or consider using a load balancing system (it will cost a lot of money)

3. Don’t think that content compression is not important. In a 100Mb/s LAN, your PHP applications may perform well, but users with slow modems must be considered.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314745.htmlTechArticlePhp setting problem;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;; ; Error handling and logging ; ; error_reporting is a bit-field. Or each number up to get desired error ; reporting level ; E_ALL - All...
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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 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)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles