Home  >  Article  >  Backend Development  >  Maximum execution time problem of PHP script_PHP tutorial

Maximum execution time problem of PHP script_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:32:531158browse

Most PHP code does not take long to execute. But sometimes, such as waiting for pictures to be uploaded, the execution time may be too long and cause a timeout.

The default maximum execution time in php.ini is 30 seconds, which is specified by the max_execution_time variable in php.ini. If you have a job that takes a long time to complete, such as sending a lot of emails to For a large number of recipients or heavy data analysis work, the server will forcibly terminate the executing program after 30 seconds. How to solve this problem?

Of course the easiest thing is to modify the value of max_execution_time in php.ini. However, not everyone has the right to modify php.ini. For example, developers who use web hosting. The php.ini on the server is used by many websites, so it cannot Feel free to modify it.

Another way is to add ini_set('max_execution_time', '0') to the PHP program. A value of 0 means there is no execution time limit, and your program can run as long as it needs to. If your program is still in the testing stage, it is recommended that you set the time limit to a real number to prevent program errors from crashing the server.

	
<?php
	//max_execution_time=100;
	ini_set("max_execution_time", 1);  //用此function才能真正在运行时设置
	for($i=1; $i< 100000; $i++) 
	{ 
		echo "No. {$i}\n"; 
		echo '<br />';
		flush(); 
	}
?>

At the same time, you can use ini_get to save the original max_execution_time setting, and restore the original setting value when the operation is completed.

Just record it here~

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752584.htmlTechArticleMost PHP code does not take long to execute. But sometimes, such as waiting for pictures to be uploaded, the execution time may be too long and cause a timeout. The default maximum execution time in php.ini is 30...
Statement:
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