string ini_get ( string varname)
string ini_set ( string varname, string newvalue)
void set_time_limit ( int seconds)
Once a script is running, you still have control over a number of system attributes that affect the way your script is executed. Using the ini_set() function you can temporarily change the execution environment, as if the php.ini file was different. Note that your change is only in effect for the current script, and will revert back when the script ends.
To use ini_set(), pass it a string for the value you want to change as its first parameter, and the new value to use as its second parameter - if successful it will return the previous value.
Here are some examples to get you started:
<?php print ini_set("max_execution_time", "300") . "<br />"; print ini_set("display_errors", "0") . "<br />"; print ini_set("include_path", "0") . "<br />"; ?>
If you want to read a php.ini value without altering it, use ini_get(), which takes the name of the value to read as its only parameter. Note that boolean values returned by ini_get() should be typecasted as integer because otherwise false values will be returned as an empty string. Here is an example of that in action:
<?php print "Display_errors is turned on: "; print (int) ini_get("display_errors"); ?>
Note that many numerical values in php.ini are represented using M for megabyte and other shortcuts - these are preserved in the return value of ini_get(), which means you should not rely on these values to be plain numbers.
If you find yourself regularly using ini_set, you might find it best just to have multiple .ini files that record your set of changes. In this situation, try using the php_ini_loaded_file() function to check which configuration is loaded - it returns a string with the .ini filename.
The set_time_limit() function lets you arbitrarily set how long a script should take to execute. This value is usually set inside php.ini under the max_execution_time setting, however you can override that here. The function takes one parameter, which is the number of seconds you want the script to have, or you can pass 0 which means "let the script run as long as it needs". Here it is in action, setting the script execution time to 30 seconds:
Note that most web servers have their own time limit. In Apache, for example, this is set under "Timeout" in httpd.conf, and defaults to 300 seconds . If you use set_time_limit() to a value greater than Apache's timeout value, Apache will stop PHP before PHP stops itself. Also note that PHP may let some scripts go over the time limit if control is outside the script. For example, if you run an external program that takes 100 seconds and you have set the time limit to 30 seconds, PHP will let the script carry on for the full 100 seconds and terminate immediately afterwards. This also happens if you use the sleep() function with a value larger than the amount of time the script has left to execute.
Author's Note: the script time limit specified in php.ini or using set_time_limit() is also used to specify the number of seconds shutdown functions have to run. For example, if you have a time limit set to 30 seconds and have used register_shutdown_function() to set up functions to be called on script end, you will get an additional 30 seconds for your shutdown functions to run.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: User functions >>
Previous chapter: Connection-related functions
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.