It ain't pure PHP (the templating syntax then), but it works realy nice; Smarty.
For loops you can do:
{foreach from=$var name=loop item=test}
{if $smarty.foreach.loop.first}- This is the first item
{/if}
- {$var.name}
{if $smarty.foreach.loop.last}- This was the last item
{/if}
{/foreach}