homepage Welcome to WebmasterWorld Guest from 54.226.0.225
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Calling a session variable multiple times on a page
is it efficient?
deejay




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


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.

 

Anyango




msg:4178383
 6:48 am on Jul 29, 2010 (gmt 0)

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'";

Readie




msg:4178408
 8:01 am on Jul 29, 2010 (gmt 0)

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.

penders




msg:4178429
 8:56 am on Jul 29, 2010 (gmt 0)

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.

Matthew1980




msg:4178450
 9:57 am on Jul 29, 2010 (gmt 0)

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

penders




msg:4178462
 10:18 am on Jul 29, 2010 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved