mds1256 Posted October 18, 2010 Share Posted October 18, 2010 Hello I have a question (that i think i know the answer to). What would be more efficient: 1. Create a global variable and run a php function and store result in there and call back the variable when needed 2. just echo the return value of the function when needed Now im guessing that calling the function once and storing that in a variable rather than keep calling the function is more efficient Quote Link to comment https://forums.phpfreaks.com/topic/216162-efficency-of-php-function-variable/ Share on other sites More sharing options...
mds1256 Posted October 18, 2010 Author Share Posted October 18, 2010 The reason why im asking is, i am wanting to create a php function to return some data from a mysql database using a select statement but this data may be echo'ed many times throughout the site e.g. a name, job title, DoB etc. Or should i use session variables so calling the function once to return the data and store this in a session and call back the session variable each time it needs echoing. What are the pro's and cons of the above Quote Link to comment https://forums.phpfreaks.com/topic/216162-efficency-of-php-function-variable/#findComment-1123394 Share on other sites More sharing options...
JonnoTheDev Posted October 18, 2010 Share Posted October 18, 2010 1. Create a global variable and run a php function and store result in there and call back the variable when needed Bad, bad, bad. Global variables are to be avoided like the plague. Functions are designed to be called whether you call it once or a hundred times in a script. If you want to change the value of a variable outside the scope of a function you can pass by reference. See the following <?php function foo($x) { return $x+1; } $y = 0; $y = foo($y); print $y."<br />"; $y = foo($y); print $y."<br />"; $y = foo($y); print $y."<br /><br />"; /* pass by ref */ function bar(&$x) { $x = $x+1; } $z = 0; bar($z). print $z."<br />"; bar($z). print $z."<br />"; bar($z). print $z."<br />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/216162-efficency-of-php-function-variable/#findComment-1123395 Share on other sites More sharing options...
JonnoTheDev Posted October 18, 2010 Share Posted October 18, 2010 The reason why im asking is, i am wanting to create a php function to return some data from a mysql database using a select statement but this data may be echo'ed many times throughout the site e.g. a name, job title, DoB etc. There is no issue with this as long as you have a properly indexed database table. If you need to obtain the same data on various pages then running the query on every page from a function call is perfectly acceptable. MySQL will cache the query so if the same query is called again it will be much faster. Or should i use session variables so calling the function once to return the data and store this in a session and call back the session variable each time it needs echoing. I would store the data in a session if it is part of an authentication process i.e the user has to login. Once their username/password has been accepted, all the information for that user could be put into a session so you do not need any further queries. What you must consider is that if a users data changes i.e they can update their details or email address, you must update the database aswell as the session variable. Quote Link to comment https://forums.phpfreaks.com/topic/216162-efficency-of-php-function-variable/#findComment-1123397 Share on other sites More sharing options...
mds1256 Posted October 18, 2010 Author Share Posted October 18, 2010 Ok understood Many thanks for taking the time to reply Quote Link to comment https://forums.phpfreaks.com/topic/216162-efficency-of-php-function-variable/#findComment-1123401 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.