Looping quirk: fields with no value are re-used from previous item

I'm looping through recent items and notice that, when a file lacks a certain field I'm looking for (say collection or creator), it uses the value from the previous item in the for loop.

Custom function for looping:

/*
** Simplifies Omeka's looping syntax
*/
function get_item_obj($itemsArray,$num=1){
	if($itemsArray){
		$myItems=get_records('Item',$itemsArray,$num);
		set_loop_records('Items', $myItems);
		$items = get_loop_records('Items');
		return $items;
	}
}

Use on homepage, amidst a number of other item loops:

<?php $items=get_item_obj(	array('recent'=>true), 5 );?>
<?php
if($items){
foreach($items as $item){
	$creator=metadata($item,array('Dublin Core','Creator'));
	$collection=get_collection_for_item($item);
?>
	<article>
	<div class="inner">
		<h3><a href="<?php echo record_url($item);?>"><?php echo metadata($item,array('Dublin Core','Title'));?></a></h3>
		<?php if($creator){
		$creatorText= '<span class="byline">Creator: <span class="creator">'.$creator.'</span></span>';
		};?>
		<?php if($collection){
		$collectionText = '<span class="byline">Collection: <span class="creator">'.metadata($collection,array('Dublin Core','Title')).'</span></span>';
		};?>

		<?php if($creatorText){
			echo $creatorText;
		}elseif($collectionText){
			echo $collectionText;
		}?>

	</div>
	</article>
<?php } } ?>

Where have I gone wrong and/or what am I missing?

Thanks -- E

Looks like the $creatorText and/or $collectionText variables are not reset each time through the loop, and so if there's not one for the current item, their values are still set from the previous item

Yes, I should have been testing for $creator and $collection in that last bit. (Really, I'm not sure why it was separate to begin with).

Thanks!