Jump to content

Efficency of php function / variable


mds1256

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 />";
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.