Jump to content

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/308468-assign-session-variables-best-practices/
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.

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.