Jump to content
StevenOliver

Assign Session Variables: Best Practices

Recommended Posts

Scenario: My page generates several PHP variables. Some of those variables get posted to another page.

Question: If a variable is to be assigned to a Session, is there any reason to assign this value to a regular PHP variable?

Example: When an API generates a "shipping label tracking number, which is better, "A" or "B" or subtle variation "C":

A.) 
$result = json_decode(curl_exec($ch));
$_SESSION["trackingNumber"] = $result->parcelTrackingNumber;

... and then use $_SESSION["trackingNumber"] throughout the rest of my script (like this: echo  "Hello, here's your tracking number: ".$_SESSION["trackingNumber"]).

B.) 
$result = json_decode(curl_exec($ch));
$trackingNumber = $result->parcelTrackingNumber;
$_SESSION["trackingNumber"] = $trackingNumber;

... and then use "$trackingNumber" throughout the rest of my script (like this: echo  "Hello, here's your tracking number: ".$trackingNumber).

C.) 
$result = json_decode(curl_exec($ch));
$_SESSION["trackingNumber"] = $result->parcelTrackingNumber;
$trackingNumber = $_SESSION["trackingNumber"];

... and then use "$trackingNumber" throughout the rest of my script (like this: echo  "Hello, here's your tracking number: ".$trackingNumber).

Thank you! I've always wondered about this. Which is best?

 

 

Share this post


Link to post
Share on other sites

There are basically two philosophical reasons you would consider the variable:

1. You don't want to type $_SESSION["trackingNumber"] everywhere. B/C are identical.

2. You need the value in the script but also need it in the session. B is more suggestive of that (you need the value later, adding it to the session is a secondary concern) but C is perfectly fine too, it's a negligible difference.

If neither of those apply then you might as well just stick with A.

Share this post


Link to post
Share on other sites

B/C can be instead written as:

$result = json_decode(curl_exec($ch));
$trackingNumber = $_SESSION["trackingNumber"] = $result->parcelTrackingNumber;

Creating a different local variable rather than using the session (or post/get/whatever) array can be useful to reduce typing.  This is the primary reason I do such things from time to time. 

Even with an editor that has good auto-complete it is frequently easier/faster to reference a simple variable rather than an array key.

Share this post


Link to post
Share on other sites

Thank you both!
I like the "$trackingNumber = $_SESSION["trackingNumber"] = $result->parcelTrackingNumber;" line of code!

I remember back in 1999 when server RAM was at a premium... assigning variables when not absolutely necessary was a no-no. And, when Sessions were invented, they made your page really, really slow. LOL

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.