StevenOliver Posted March 15, 2019 Share Posted March 15, 2019 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? Quote Link to comment https://forums.phpfreaks.com/topic/308468-assign-session-variables-best-practices/ Share on other sites More sharing options...
requinix Posted March 15, 2019 Share Posted March 15, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308468-assign-session-variables-best-practices/#findComment-1565253 Share on other sites More sharing options...
kicken Posted March 16, 2019 Share Posted March 16, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308468-assign-session-variables-best-practices/#findComment-1565257 Share on other sites More sharing options...
StevenOliver Posted March 16, 2019 Author Share Posted March 16, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/308468-assign-session-variables-best-practices/#findComment-1565259 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.