PHP upgrades echo, printf, print, file

Source: Internet
Author: User
To make the ECHO faster, let the PHP request processing process end as soon as possible. The reason why the ECHO is slow is to wait for the data to be written to be returned successfully, then a simple method is to open the output cache and edit php. inioutput_buffering4096bytes can also be displayed in the script by calling ob_start (): ob_start (); echo $ huge_string;

To make the ECHO faster, let the PHP request processing process end as soon as possible. The reason why the ECHO is slow is to wait for the data to be written to be returned successfully, then a simple method is to open the output cache and edit php. ini output_buffering = 4096 // bytes can also be displayed in the script by calling ob_start (): ob_start (); echo $ huge_string;

To make the ECHO faster and let the PHP request processing process end as soon as possible, the ECHO is slow because it is waiting for the "Write Data" to be returned successfully. A simple method is to open the output cache,

Edit php. ini

output_buffering = 4096 //bytes

You can also call ob_start () in the script ():

Ob_start (); echo $ huge_string; // other logic. ob_end_flush ();

Note that ob_start will open up a buffer with a size of 4096. Therefore, if the value of huge_string is greater than 4096, it will not accelerate.

Now, our ECHO will "immediately" run successfully and return. because the data is temporarily written to our output cache. if the buffer size is large enough, the content will be sent to the client (strictly speaking, it is sent to WebServer) at the end of the script ). however, this does not solve the problem we encountered today, because the data needs to be sent to the client by PHP (the Output buffer of WebServer is not considered at this time) at the end ), this process will not end, the request will not be closed, and PHP will not execute DB destructor.

If PHP needs to output 100 kb of data, the Apache output cache must be greater than KB. Otherwise, when the Apache output cache is full, it will be actually sent to the client, in this process, the ECHO executed at that time will block the waiting.

So how can I modify the Apache output cache? We can use the SendBufferSize configuration command in the apache configuration file.

SendBufferSize 4096 // note that it is byte

Detailed SendBufferSize description, see the http://httpd.apache.org/docs/2.0/en/mod/mpm_common.html#sendbuffersize

Note: For other Webserver with php-cgi modes, read the relevant Webserver manual to find similar configurations.

Now, PHP's ECHO will directly deliver the content to Apache. After PHP completes the execution, it will not wait until the content is sent to the client and quit directly. the content will be sent to the client by Apache after PHP processing is complete. this accelerates the ECHO execution efficiency.

Printf, print, file_put_contents ("php: // output ")... And so on. It's the same as ECHO.

Finally, it should be noted that, in this way, only the waiting time of the original ECHO is transferred to Apache, and the time when the client obtains the content is not actually reduced. it only accelerates the PHP processing process and advances the exit time of PHP, which can reduce the time occupied by PHP resources and indirectly increase the resource usage.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.