I can't provide much in the way of statistics, but I can provide some knowledge on the gist of what's going on underneath, to help decide when to use a variable and when not to. This is fairly basic stuff, so many of you will probably know it already, but hopefully it'll help those who don't.
When you create a variable, it reserves a space in the computer's memory for that variable's value. In example 1 below, you will be using more memory than example 2:
PHP Code:
// Example 1
$loggedIn = isLoggedIn();
if($loggedIn){
// do stuff...
}
// Example 2
if(isLoggedIn()){
// do stuff...
}
The amount of memory per variable extremely low (probably never more than a few bytes), but this can add up over a long script spanning multiple functions (each with variables of it's own). Therefore, it's a good idea to not use variables when they don't help anything. That being said, there are three cases when you SHOULD use variables.
1. When you need one. This one is obvious. Some stuff, you can't do without a variable (like increment a counter, etc.). Don't look for ways around using a variable; as I said, each individual variable adds almost nothing, so you don't have to feel guilty about using them when you need them.
2. When avoiding one would make something slower. This is similar to what 8nerd said. There are a lot of cases where you end up calling a function over and over again, and the result never changes (like calling count($array) in the head of a for loop). Calling the function once, assigning the answer to a variable, and checking that variable will save the machine from calling the function N times (where N is the number of times the loop happens).
3. When it makes development easier for you. This one is very important, and depends on personal preference. The original example in this thread illustrates in best:
Example 1
PHP Code:
$host = 'localhost';
$dbUser = 'x';
$dbPass = 'x';
$dbName = 'x';
$conn = @mysql_connect($host, $dbUser, $dbPass);
if (!$conn) {
echo 'Unable to connect to the database server at this time.';
exit();
}
$choose = @mysql_select_db($dbName);
if (!$choose) {
echo 'Error selecting database.';
exit();
}
mysql_query($conn);
mysql_query($choose);
Example 2
PHP Code:
mysql_connect('localhost','x','x') or die('Critical server error');
mysql_select_db('x') or die('Critical server error');
While Example 2 will go a bit faster (it uses no variables, while Example 1 uses 6), that "bit" is negligible. What's important here is, which one makes your life easier?
Some people would prefer Example 1; it's clear what everything is by the variable names, you can change values without looking through the functions, and you can reuse the values in other functions.
Others would prefer Example 2; it's smaller, so it's easier to glance at and immediately figure out what's happening for many people. Choose whichever one makes your life easier.
However, if you have no preference (which may start to happen as you use PHP more), pick Example 2. It's a slight bit faster, and if you don't care either way, there's no reason not to pick it.
One final note: when you're dealing with database resources, ALWAYS assign their results to a variable. If you're running the same database query in every iteration of a loop, you're wasting a massive amount of resources unnecessarily.
Bookmarks