2

Before starting, I'm not asking about standard coding practice or "etiquette." My question is more from curiosity with the internals of PHP. My research so far mostly seems to find people confused about scope in PHP.

Does re-using variables come with any benefit/detriment in PHP, either in memory or speed? For science.

Say you are sequentially accessing multiple files throughout a script.

  • Scenario A: your handles each have a variable, $file1, $file2, $file3, etc.
  • Scenario B: your handles all reuse the variable $fp

Will this theoretical scenario require respectively resource intensive scripts to matter? Will B allow garbage collection to get rid of the old handles while A won't? Will optimization through Zend make this a non-issue either way?

3
  • 1
    IIRC, PHP's garbage collector removes variables from memory once they go out of scope. You won't make a huge difference by freeing one variable. Commented Jun 20, 2012 at 0:11
  • My hunch is that B uses slightly less memory but both are probably poor coding practices. It sounds like what you want is to write a function that does something with these files and call it three times. Don't just dump everything into one big scope. Write small, manageable functions and just let unneeded variables go out of scope when the function ends. Commented Jun 20, 2012 at 0:34
  • Because you asked about file handles here, you might want to look it from the other way round: what happens if you close the file with the variable? Can I close a file by unsetting the handle? Commented Jun 20, 2012 at 0:44

2 Answers 2

4

There is not a cut & dry answer to this question. Optimization and performance will depend heavily on your particular codebase, the platform it runs on, what else is running on the server, and more.

To start with your scenarios are too vague to provide an adequate answer. However, to touch on some of the prior comments/concerns...

PHP does not have very well defined rules for garbage collection. In THEORY scenario A will release the memory when a function exits thanks to garbage collection. In reality this rarely happens. There are a number of triggers that will cause garbage collection to release that memory, but behind the scenes the actual low-level free() and mallocs() are not cut & dry. If you watch your memory stack closely you will find that after a function exit the memory space for $file1, $file2, $file3 will remain. Sometimes until the entire application exits.

Your application construction will also determine which is faster, creating a new entry in the symbol table for $file1, $file2, $file3 or re-using $fp over & over. Re-using $fp, again IN THEORY, would typically mean the memory space does not need to be re-allocated and a new symbol table entry and corresponding management object does not need to be re-created. However this is not always the case. Sometimes re-using $fp actually can be slower because a destroy needs to be called first, then re-creating the object. In some corner cases it may be faster to just create a new $file1, $file2, $file3 on the iterative process and let garbage collection happen all-at-once.

So, the bottom line of all this....

You need to analyze and test your own apps in their native environment to learn how things behave in YOUR playground. It is rarely an "always do this" or "never do that" scenario.

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed. I knew it would be a nit-picky complicated issue. I appreciate your analysis of the two scenarios. I think you understood what I was trying to get at in terms of general memory usage/performance. Any knowledge about how Zend might complicate it even more? Generally, it simply claims to "[go] over the intermediate code generated by the standard Zend run-time compiler and [optimize] it for faster execution." I wonder how using variables in this way might affect things. Seems like it might if you're reusing with the same datatype...
0

Not confident on my answer, but I do found that reuse vars saves more memory, especially when re-using vars for query results as often time those vars will fill with a lot of other unwanted stuff in there.

You can use

echo memory_get_usage() at different stage of the code execution to see the difference and compare.

But it could get confusing as your code grows and makes it harder for people to read.

Also PHP runs garbage collection when the script is done. so how you name your vars probably won't have anything to do with it, rather it effects how much memory it uses during execution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.