22

I've had index.php and several files which cascading include,something like this.

index.php -> controller.php -> model.php -> view.php

In model.php I have a function using ini_set('memory_limit', '-1');

When will the ini_set() change of the setting expire?

After executed index.php? Or view.php? Or the function in model.php?

hakre
  • 193,403
  • 52
  • 435
  • 836
Persk
  • 639
  • 2
  • 6
  • 15

1 Answers1

27

ini_set() is global for everything that happens in the script (not just the current file: the whole thread of execution that is occurring), for this entire one request; it doesn't matter whence you invoke it, it will always affect the global settings for this script. The effect will expire when your script terminates - e.g. through exit, die, or running off the end of index.php.

It will not affect any other scripts running simultaneously (those need to call ini_set themselves), and it will not persist into later requests (if you need persistent settings, you need to edit php.ini).

Note that the documentation says the same thing:

Sets the value of the given configuration option. The configuration option will keep this new value during the script's execution, and will be restored at the script's ending.


Edit: Since it is apparently unclear: the value you change using ini_set will be valid for the whole script onwards. It doesn't matter where the execution currently is (in what file, in what class, in what function); the setting will be the same, everywhere. It will remain so until you change it again, or until the whole script terminates. (not the current file, not the current function; the whole script)

Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
  • I've already read the document but I wonder will it effect in function scope(like variable) or the entire script after using this function.I'm not sure what their "script" means. – Persk Jun 27 '11 at 09:52
  • @thReality: Did you read my answer? Quote: "it doesn't matter whence you invoke it, it will always affect the global settings for this script" - in other words, completely regardless of function scope. – Piskvor left the building Jun 27 '11 at 10:26
  • "it doesn't matter whence you invoke it" it matters when you invoke it, "ini_set will be valid for the whole script onwards."; – MTVS Nov 26 '12 at 15:29
  • @csstd: And? "whence" != "when". "Whence", adj. 1. From which place. http://www.thefreedictionary.com/whence – Piskvor left the building Nov 27 '12 at 15:37
  • Do you have a reference for this? – Darth Egregious Feb 22 '13 at 19:11
  • 1
    @user973810: Link to PHP documentation is not reference enough for you? Sorry, I have nothing more authoritative than that. – Piskvor left the building Feb 25 '13 at 10:06
  • Maybe I'm not reading it correctly, but I find the documentation's wording to be vague. Thanks though. – Darth Egregious Feb 25 '13 at 16:19
  • It is indeed somewhat vague; I don't have anything better than the documentation (and observation which is completely consistent with it), though. – Piskvor left the building Feb 25 '13 at 17:00
  • 4
    "during the script's execution" is not obvious if you aren't sure what is meant by "the script". If I define a class in "myclass.php" I might consider "myclass.php" to be a PHP "script", and if I call ini_set in a function in that class, I might incorrectly guess that the value is restored outside of my "script" function call. So unfortunately by repeating the words "the script" over and over again the answer doesn't become more clear if the person doesn't know that "the script" is referring to the whole thread of execution that is occurring. – AndyClaw Aug 25 '14 at 18:18
  • I wonder what the execution scope when this isn't called at the top of the script? – Kellen Stuart Oct 19 '16 at 20:11
  • @KolobCanyon: The notion of scope doesn't quite apply here, this is not a variable. Doesn't matter where you call this: from that point, in this current running script, the new value applies everywhere. (Is "global" unclear?) – Piskvor left the building Oct 20 '16 at 22:00
  • @Piskvor First of all, variables are not the only entities that have scope in Php - functions for one have a scope; `ini_set() is a function and therefore has scope... it absolutely applies here. The question, which you unkindly did not answer, was if Php would still take the `ini_set()` into effect even if it was the last instruction in the script and apply that to the commands above it, which now I'm pretty sure php takes everything one line at a time... so the answer to my question is no. – Kellen Stuart Oct 25 '16 at 16:25
  • @Kolob Canyon: If "the value you change using ini_set will be valid for the whole script onwards" in the answer and variations in the comments are not clear enough or kind enough for you, then I have no better way to explain this. I suggest that you try someone else to pester: I've explained it to the best of my ability, there is nothing more that I could do. – Piskvor left the building Oct 28 '16 at 12:33
  • So if you set X memory_limit using ini_set on a script and you run it twice simultaneously, does that mean the scripts will allocate 2X memory? – logicbloke Sep 03 '21 at 14:36