14

How do I use ini_set('max_execution_time', 0);?

Question 1: Do I place it at the top of the .PHP file or in a function that takes a long time to do something?

Question 2: Does this setting last forever after being set? Or does it return back to its original 300sec or whatever default value after the function stops running?

Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
  • 1
    Changes via `ini_set()` are per-script-execution. It applies only to that script, and only while it's running. It will not change global PHP settings for other scripts. – Marc B Jun 29 '11 at 20:16
  • A little bit OFF, but you can find some useful information about setting `max_execution_time` to zero: http://stackoverflow.com/questions/4306605/is-ini-setmax-execution-time-0-a-bad-idea. – Sk8erPeter Mar 26 '12 at 14:27

3 Answers3

20

You can place it anywhere, but that setting won't take effect until it runs. So if you put it at the top, then the script will never timeout. If you put it down below on the function that can take awhile, then you may get a timeout above if the script takes a long time to get to where you called it.

When you use ini_set() that option stays in effect for the entire execution of the script.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • I suppose if I put `ini_set()` at the start of a PHP function that takes very long to finish its task, that option stays till the function returns to its caller? – Nyxynyx Jun 29 '11 at 15:32
  • 1
    @Nyxynyx no -- it stays until the whole script is fully done loading – Naftali Jun 29 '11 at 15:34
  • @nyxynyx, think of `ini_set()` as setting a global option. It doesn't matter where you are calling it. You are setting a PHP option on the engine, not your script. No matter where you call it (in a function, not in a function) that setting is then set for the entire execution of your script, start to finish, regardless of scope. – Brad Jun 29 '11 at 15:38
4

2: It only last for that page that loaded. after that its done.

1: It should be placed at the start of the code, but it can be placed anywhere.

Naftali
  • 144,921
  • 39
  • 244
  • 303
1

Answer 1: before you execute the long-running code

Answer 2: it lasts until the php process ends

cweiske
  • 30,033
  • 14
  • 133
  • 194