Best practices for printing out an array

I have created a method called breadcrumbs() that returns an array.

I have noticed when I output the array as per the first example below, I am getting unwanted white space in the browsers I have tested (Firefox, Chrome, Safari).

First Example:


<div>
  <? foreach($kb->breadcrumbs() as $key => $value){ ?>
    <a href="<? =$key ?>"><? =$value ?></a>
  <? } ?>
</div>

But if I handle the array like this second example, no unnecessary whitespace appears.

Second example:


<div>
  <? foreach($kb->breadcrumbs() as $key => $value){
    echo '<a href="'.$key.'">'.$value.'</a>';
  } ?>
</div>

What am I doing wrong in the first example? I thought the first way was the preferred method as it is better at separating the PHP logic from the HTML presentation?

<div>
  <? foreach($kb->breadcrumbs() as $k=>$v){ ?><a href="<? =$ ?>"><? =$v ?></a><? } ?>
</div>

Remove the line breaks if you don’t want them to appear.

Either format your code to suit you, or to suit your output, or break the separation of PHP/HTML rule which you seem to have adopted.

(shortened key and value to k=>v for better readability - I think that generally many coders understand k and v used in an array will mean key and value, likewise n for number and i for counter)