When I use file_get_contents and pass it as a parameter to another function, without assigning it to a variable, does that memory get released before the script execution finishes?
For Example:
preg_match($pattern, file_get_contents('http://domain.tld/path/to/file.ext'), $matches);
Will the memory used by file_get_contents be released before the script finishes?
The temporary string created to hold the file contents will be destroyed. Without delving into the sources to confirm, here’s a couple of ways you can test that a temporary value created as a function parameter gets destroyed:
Method 1: a class which reports its destruction
This demonstrates lifetime by using a class which reports on its own demise:
This outputs
Which indicates that the implied temporary variable is destroyed right after the foo function call.
Method 2: measure memory usage
Here’s another method which offers further confirmation using memory_get_usage to measure how much we’ve consumed.
This outputs