Web Hosting Talk







View Full Version : Efficient php coding?


MGCJerry
05-07-2005, 09:12 AM
I've gotten back into some PHP coding, and I'm beginning to wonder if my assumptions are correct about efficiency and how server friendly it is.

Is it a good thing to unset any variables that are no longer needed after I've finished utilizing them in a particular area of code? Is this good coding practice, or am I just being overly critical?

example...

// In this example, I'm using a database class...
$sql = "SELECT * FROM table WHERE field = '$var'";
$data = $db->sql_fetchrow($source);
unset($sql);

//working with the returned data...
//... blah, blah, blah...

unset($data);

//... Continuing with script...


Thoughts?

absolut
05-07-2005, 09:17 AM
If you use only once $sql and $data in script than you don't need to unset them.

MGCJerry
05-07-2005, 09:25 AM
Thanks for the speedy reply.

Yes, they will only be used once. So, there is really no need to unset them?

error404
05-07-2005, 09:32 AM
Unset will free the memory used by the structure for use elsewhere on the server or in your program. Normally, with variables such as those, this shouldn't be a huge consideration, and most of the time it's not worth using unset. It can, however, come into effect when you have large data structures stored in memory - you can significantly reduce the memory burden of your application by freeing temporaries and the like. Also, it can help you if you run into the memory_limit.

I'd suggest most of the time it's not necessary, but for any large structure, it's probably a good idea. Also, use references as much as possible when working with large data sets.

MGCJerry
05-07-2005, 12:46 PM
Actually, the variable in this example is an array with some 60 or so values. ($data[value1], $data[value2], etc), and most of them will have a fair amount of text in them, but should be no more than 65,535 charcters each value.

Anyway, what do you mean by "freeing temporaries"?

error404
05-07-2005, 05:49 PM
For example, something like this contrived, unrealistic example:

$result = $db->getAll($query);
$special_record = $result[12]; // I don't know why record 13 is important, but it is.

Using unset() on $result will free the space occupied by that variable, which was only ever intended to have a temporary purpose. Many times you'll use variables as intermediate storage during a process, and that can leave copies of large sets of data you don't need floating around.

That's quite a large array, I'd unset it when you're done with it, unless that's just at the end of your script.

MGCJerry
05-07-2005, 06:30 PM
Ahh, in that case I avoid using temporary variables if at all possible. It makes the code look un-necessarily complicated and it can confuse me easily when I have a bunch of variables with the same data but under a different name.

//For example, I'm more inclined to do this:
echo $db->sql_numrows($db->sql_query($sql));

//than do to:
$result = $db->sql_query($sql);
echo $db->sql_numrows($result);


Also, using your example, I would have never made result13 into another variable, but I do make sure that the variable does make sense (eg $customter[name], $customer[address], etc).

As of right now, I have unset all "$sql" variables, and my data variables when I'm done working with them.

Thanks again for the replies. So it seems that there is really no real difference in unset()'ing unless using a large variable thats an array.

error404
05-07-2005, 07:29 PM
Originally posted by MGCJerry
Thanks again for the replies. So it seems that there is really no real difference in unset()'ing unless using a large variable thats an array. [/B]

Or an instance of a large object, or a long string (a file buffer, perhaps). In practical use it won't make much of a difference either way since the runtime is generally fairly short.

As for code style, sometimes it makes it a lot less readable to compress 12 function calls into a single line of code ;). PHP uses copy-on-write, so if you don't actually modify the data, the cost of assigning it to a variable is very low. When the data is changed, a copy is then made to store the new version at a new location. You still need to unset them though, or when the original variable you copied goes out of scope or is otherwise deallocated, there might still be a reference to the data lying around, and PHP will keep it in memory.

Anyway, don't bother unless you think it might be a problem, or you want to ensure that commonly used variables (e.g. $sql) have a consistent state whenever you go to use them.

Cap'n Steve
05-08-2005, 02:35 AM
Using unset() is probably not worth it unless you have huge variables.

However, if you're fetching multiple row from mysql, you should probably use mysql_free_result() once you're done with the data.

JieWei
05-08-2005, 03:37 AM
I agree with Steve.... sql_free_result() would be a better option for you, incase if there are huge chunks of results...