In PHP is it considered good practise to store things like cookie or session information inside variables instead of callling them over and over. For example:
$happiness_level = $_SESSION['happiness_level'];
echo $happiness_level.' something';
echo $happiness_level.' something else';
vs:
echo $_SESSION['happiness_level'].' something';
echo $_SESSION['happiness_level'].' something else';
Is the 2nd one worse for performance. I ask because in jQuery it's good performance practice to cache selectors by assigning them to variables so the DOM doesn't have to be traversed each time the selector is called.
I was wondering if a similar rule applies in PHP. Or does PHP recognize internally that it already grabbed the session variable named "happiness_level" so the 2nd time it's called, instead of doing all the extra work of looking up it's value again, it instead just uses the original value from the 1st call?
Basically which is better (example 1 or 2) for performance, even if we're talking milliseconds?