Last updated 4 June 2005. Created on 3 June 2005.
Edited by Dublin Drupaller, laura s. Log in to edit this page.

If you are using two or more php snippets in the same node - or on your front_page.module page - add the following line at the beginning of each php snippet you intend to use

unset ($output);


The reason is to avoid the problem of using the return $output; more than once in the same node. The return $output command will end the reading of code and all subsequent code or text in that node (or front_page) will be ignored.

To avoid potential problems, rather than return $output;, you can use... print $output; ...for the last line of code.


With each subsequent php-generated list you create in that same node (or front_page), you will get a cumulative list -- the content generated by each additional php snippet will be added to the $output list. (For example, the second php snippet's list of posts on "apples" would be added to the first php snippet's list of posts on "oranges," when what you really want is just a list of posts on "apples.")

So how would one "clear" the output results so one can have another php snippet creating output? Start each of your subsequent snippets of php code with this:

unset ($output);

This will clear the previous results so that your 2nd, 3rd ... nth list will have unique results relevant to its own query.

Thus you will be able to have several php-generated outputs on the same page (such as lists of recent posts in several different taxonomy terms) without conflict or intermingling of results.

Looking for support? Visit the forums, or join #drupal-support in IRC.


lanexa’s picture

I'm using the following snippet 3 times on a custom front_page. This is the third of the 3, and contains the unset ($output); line. This is a 5.1 site and I'm still getting cumulative results. Any help is greatly appreciated.

unset ($output);

// tag to use:
$tag = 'Article';

// maximum number of items to show:
$count = 6;

$result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title
FROM {node} n
INNER JOIN {term_node} tn ON n.nid = tn.nid
INNER JOIN {term_data} td ON tn.tid = td.tid
WHERE = '%s' AND
n.status = 1
ORDER BY n.created DESC"),$tag,0,$count);

while ($node = db_fetch_object($result)) {
  $items[] = l($node->title, 'node/'. $node->nid);
$output = theme('item_list', $items);
print $output;
lanexa’s picture

nevets posted the solution here -

 unset ($output); 
 unset ($items);