php method to implement scheduled tasks: first create a new php file; then use the ignore_user_abort method to set the program to execute when the browser is closed; then use set_time_limit(0) to set the response time of the program to be unlimited; finally pass Sleep and other methods can implement scheduled tasks.
The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer
Create a new php file and name it test.php. Used to explain how to set up scheduled tasks in PHP.
In the test.php file, use the ignore_user_abort() method to set the program to also execute the program when the browser is closed, and the parameter is true.
In the test.php file, use set_time_limit(0) to set the program's response time without limit. If there is no limit, the response time defaults to 30 seconds.
In the test.php file, use the while() method to execute the sleep() method in a loop. The sleep() method is set to 5 to execute the program every 5 seconds.
Create a new test.txt text file. In the while() method body, use the fopen() method to open the file. If the test.txt file is successfully opened, use fwrite () method writes the "file content number" content to the test.txt file. If writing content to test.txt fails, "Failed to write file" will be prompted.
Use fclose() to close the file resource and set the conditions for the termination of the scheduled task. Here, when the lock.txt text file exists, use break to stop the scheduled task. If there is no lock.txt in the same directory, the scheduled task will continue to be executed.
Run the test.php file in the browser and start executing the scheduled task, that is, writing the "file content number" content to the test.txt file every 5 seconds.
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to implement scheduled tasks in php. For more information, please follow other related articles on the PHP Chinese website!