I have a loop which is something like
$items = array(10, 12, 13, 23, 34, 32, 11, 98);
$items_single = array();
foreach ($items as $item) {
$item_data = "SELECT * FROM MY_TABLE WHERE ITEM_ID = $item";
// Don't worry, this is just a demo query, I don't query like this
array_push($items_single, $item_data);
unset($item_data);
// $item_data = NULL;
}
Which is a better way to free the $item_data
memory? While unset()
seems more logical, but I think this way php is not going to unset it right away, but will put it garabage collection sort of thing which might be called after the loop...? (which will fail the purpose of having unset()
in the first place). While = NULL
seems to be doing what would be ideal, but then it will take some constant time which might actually slow down the looping.
Also, if I don't do anything at all, then php will create a new variable every time (which will consume memory), and then try to remove my old one and replace with the new one (I guess it works this way).