Welcome to WebmasterWorld Guest from 54.159.214.250

Forum Moderators: coopster & jatar k

Calling a session variable multiple times on a page

is it efficient?

   
2:59 am on Jul 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




I have a site where once an administrator selects the user they want to view, I then put that userid into a session variable:

$_SESSION['vwuserid']

Which allows me to conveniently hang onto that variable throughout the site, until the admin selects a different user to view.

BUT within an individual page I may need to call on that variable as a query criteria 8 or 9 times, eg:

$query1 = "SELECT * FROM tablea WHERE userid='".$_SESSION['vwuserid']."'";

Question: Is it better (ie, more efficient/lighter load) to use the session variable repeatedly as above, or is there any gain in calling the session variable once and putting it in an ordinary variable, and then using that in the multiple queries, eg:

$vwuserid = $_SESSION['vwuserid'];
$query1 = "SELECT * FROM tablea WHERE userid='".$vwuserid."'";
$query2 = "SELECT * FROM tableb WHERE userid='".$vwuserid."'";

Any thoughts? I'd really like to tie this baby down as tight as I can.
6:48 am on Jul 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



imho it cant be a considerable difference, if at all there is a difference. but the reason i would go with the variable idea is that it makes the code easier to read and to write, for example

$query1 = "SELECT * FROM tablea WHERE userid='".$_SESSION['vwuserid']."'";

would be

$query1 = "SELECT * FROM tablea WHERE userid='$vwuserid'";
8:01 am on Jul 29, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



To be fair, there is a counter argument to that Anyango - $_SESSION['foo'] stands out from the code a bit more than $foo.

Also, declaring the same variable twice is going to cause the script to consume more memory.
8:56 am on Jul 29, 2010 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Also, declaring the same variable twice is going to cause the script to consume more memory.


Or, assigning one variable to another. Actually, this does not consume as much memory as you might think, if any (although I suspect there will need to be an additional entry in the symbol table?). PHP implements copy-on-write (or lazy copy), so if you assign one variable to another it does not duplicate its contents - until you actually change it.

So, if you simply copy a variable and don't change it, you will not use any more memory.
9:57 am on Jul 29, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hi all,

$_SESSION['foo'] is globally available if its set, defining a var to assign that to takes up more memory as Readie correctly states (though it is only bytes), if the $_SESSION is set, use it, makes code easier to read too, just sanitise it before using it in a sql query :)

Though if you change page - navigate to another page make sure you use session_start() again at the top of the page to access that session variable or else you will get an undefined index error being flagged

Cheers,
MRb
10:18 am on Jul 29, 2010 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Matthew1980 ...defining a var to assign that to takes up more memory as Readie correctly states...


No it doesn't - which was the point of my post above. Try it... call memory_get_usage(true) before and after the assignment and you will see no change in terms of memory usage.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month