Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a fair number of items in a session, and I am wondering if there are any advantages to copying the session variable to a normal php variable (in order to close the session file as quickly as possible) and then do any calls from there. Something like:

$copiedSession = $_SESSION;
if (isset($copiedSessionData['foo'])){
  1. I am looking for ways to test performance on this.
  2. Does it make sense to do so?
  3. Is there a break-even point where it helps / doesn't help?
share|improve this question
Why the downvote? I can't learn from a mistake unless I know what is it. Thanks. – Cymbals Apr 2 '13 at 18:47
up vote 2 down vote accepted

My PHP experience is very rusty, but in the example you provided, aren't you just creating a reference variable pointing to the session? In other words,

$copiedSession = $_SESSION;

doesn't give you a "copy" of the session, but just a second variable from which you can reach it.

You want a deep copy.

foreach ($_SESSION as $key => $value)
    $copiedSession[$key] = $value

I think that is what you want.

As for better performance, I suspect not. It depends on whether $_SESSION is a pure associative array or if it is a special file-backed map that reads/writes through in real-time.

Judging by the PHP manual's SessionHandler interface, whose read/write methods take a session id and session array, I suspect that $_SESSION is a plain old array which comes from SessionHandler::read and gets passed back to SessionHandler::write.

However, to be absolutely sure, the best way is simply to test it.

You could do reading and writing in a loop say 10,000 times against the $_SESSION variable directly and measure min, max and mean execution time. Then do the same thing against a copy (using the code I gave you above, not the reference pointer code) and compare the numbers.

share|improve this answer
Thanks for the response. If you want a reference, it would be $copiedSession = &$_SESSION. I like your idea on the performance testing. If anyone else is looking for ways to implement something like this:… – Cymbals Mar 25 '13 at 19:06
@Cymbals PHP uses copy-on-write, so $copiedSession = $_SESSION isn't actually a copy; they really are pointing at the same data structure until one of them changes - if Brandon is correct and it really is just an array. If it's a special object, I'm not so sure. – Izkata Mar 28 '13 at 0:49

It makes sense to do this when your storage engine is the file system and you have multiple scripts accessing the same session data. (ex: ajax calls). The file containing the session data will be locked until the first script ends execution and this can lead to performance issues. You can find a detailed explanation here

share|improve this answer
That is a good thing to keep in mind. – Cymbals Mar 25 '13 at 19:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.