PHP Tutorials - Herong's Tutorial Examples - v5.17, by Herong Yang
set_time_limit() - max_execution_time
This section provides a tutorial example to demonstrate the maximum execution time limit, which is defaulted to 30 seconds only. You can change the default with a set_time_limit() call, or 'max_execution_time' setting in php.ini.
If you are running PHP scripts that take longer time to finish, you may experience the "Maximum execution time of 30 seconds exceeded" error.
By default, PHP does not allow any script to run for more than 30 seconds. But you can change this behavior in several ways:
The following PHP script, Execution_Time.php, is designed to calculate the execution time and trigger the "Maximum execution time of 10 seconds exceeded" error.
<?php #- Execution_Time.php #- Copyright 2003 (c) HerongYang.com. All Rights Reserved. set_time_limit(10); date_default_timezone_set('Europe/Paris'); $limit = 1000000; $step = 1000; $time_0 = time(); $sleep = 0; $work = 0; for ($i=$step; $i<$limit; $i+=$step) { $time_1 = time (); $count = find_primes($i); $time_2 = time (); $work += $time_2-$time_1; sleep(1); $sleep++; print("Total time: ".(time()-$time_0) . ", working: ".$work.", sleeping: ".$sleep . ". Primes: $count, within: $i\n"); } function find_primes($limit) { $count = 0; $i = 3; while ($i<$limit) { $isPrime = true; $j = 2; while ($j<$i) { $isPrime = $i%$j > 0; if (!$isPrime) break; $j++; } if ($isPrime) $count++; $i++; } return $count; } ?>
If you run it, you will get the "Maximum execution time of 10 seconds exceeded" error sooner or later, depending on your CPU speed.
herong$ php Execution_Time.php Total time: 1, working: 0, sleeping: 1. Primes: 167, within: 1000 Total time: 2, working: 0, sleeping: 2. Primes: 302, within: 2000 Total time: 3, working: 0, sleeping: 3. Primes: 429, within: 3000 Total time: 4, working: 0, sleeping: 4. Primes: 549, within: 4000 Total time: 5, working: 0, sleeping: 5. Primes: 668, within: 5000 Total time: 6, working: 0, sleeping: 6. Primes: 782, within: 6000 Total time: 7, working: 0, sleeping: 7. Primes: 899, within: 7000 Total time: 8, working: 0, sleeping: 8. Primes: 1006, within: 8000 Total time: 10, working: 1, sleeping: 9. Primes: 1116, within: 9000 Total time: 11, working: 1, sleeping: 10. Primes: 1228, within: 10000 Total time: 12, working: 1, sleeping: 11. Primes: 1334, within: 11000 Total time: 14, working: 2, sleeping: 12. Primes: 1437, within: 12000 Total time: 15, working: 2, sleeping: 13. Primes: 1546, within: 13000 Total time: 17, working: 3, sleeping: 14. Primes: 1651, within: 14000 Total time: 18, working: 3, sleeping: 15. Primes: 1753, within: 15000 Total time: 20, working: 4, sleeping: 16. Primes: 1861, within: 16000 Total time: 22, working: 5, sleeping: 17. Primes: 1959, within: 17000 Total time: 24, working: 6, sleeping: 18. Primes: 2063, within: 18000 Total time: 26, working: 7, sleeping: 19. Primes: 2157, within: 19000 Total time: 28, working: 8, sleeping: 20. Primes: 2261, within: 20000 Total time: 30, working: 9, sleeping: 21. Primes: 2359, within: 21000 PHP Fatal error: Maximum execution time of 10 seconds exceeded in /Users/herong/Execution_Time.php on line 36
As you can see from the output, the maximum execution time limit applies to accumulated execution time only, not the elapsed time.
Table of Contents
Introduction and Installation of PHP
PHP Data Types and Data Literals
Variables, References, and Constants
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
►Interface with Operating System
$argv[] - Command Line Arguments
Options to Execute External Programs
exec() - Execute External Programs
system() - Execute External Programs
passthru() - Execute External Programs
popen() - Execute External Programs
proc_open() - Execute External Programs
memory_get_usage() - Memory Usage Info
►set_time_limit() - max_execution_time
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
MySQL Server Connection and Access Functions
Functions to Manage Directories, Files and Images
SOAP Extension Function and Calling Web Services
SOAP Server Functions and Examples
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Parsing and Managing HTML Documents
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS