views:

71

answers:

4

This is justa a performance question.

What is faster, access a local PHP variable or try to access to session variable?

+7  A: 

I do not think that this makes any measurable difference. $_SESSION is filled by PHP before your script actually runs, so this is like accessing any other variable.

elusive
A: 

It depends, are you talking about setting the $_SESSION variable to a local variable for use throughout the file or simple talking about the inherent differences between the two types of variables?

One is declared by you and another is core functionality. It will always be just a smidge slower to set the $_SESSION variable to a local variable, but the differenceenter code here is negligible compared to the ease of reading and re-using.

d2burke
+2  A: 

Superglobals will be slightly slower to access than non-superglobal variables. However, this difference will only be noticeable if you are doing millions of accesses in a script and, even then, such difference doesn't warrant change in your code.

$_SESSION['a'] = 1;
$arr['a'] = 1;

$start = 0; $end = 0;

// A
$start = microtime(true);
for ($a=0; $a<1000000; $a++) {
    $arr['a']++; 
}
$end = microtime(true);
echo $end - $start . "<br />\n";

// B
$start = microtime(true);
for ($b=0; $b<1000000; $b++) {  
    $_SESSION['a']++;   
}
$end = microtime(true);
echo $end - $start . "<br />\n";

/* Outputs: 
0.27223491668701
0.40177798271179

0.27622604370117
0.37337398529053

0.3008668422699
0.39706206321716

0.27507615089417
0.40228199958801

0.27182102203369
0.40200400352478
*/
webbiedave
Too bad there's no practical way to bench the session open/close overhead. Unserializing a session might be a bit more expensive than simply loading a .php file with a bunch of variable assignments in it.
Marc B
A: 

There is nothing performance-related in this question.
The only those performance questions are real ones, which have a profiling report in it.
Otherwise it's just empty chatter.

Actually such a difference will never be a bottleneck.
And there is no difference at all.

Col. Shrapnel