Printing errors in a list

I currently use the following function to print error messages if any are generated:


function print_error($key, $errors) {
if (isset($errors[$key])) {
print "<li>{$errors[$key]}</li>"; }
}

Above the form that is being processed, I have:


<ol class="checklist errors">
...error messages are inserted here in list items...
</ol>

The errors are printed inside that OL by:


<?php print_error('message', $errors) ?>
<?php print_error('name', $errors) ?>
et cetera...

My question is how do I print the ordered list only when errors are generated, so that the HTML does not need to have an empty list in it?

Before calling print you can check if there are any errors in array


<?php
if(count($errors) > 0)
{
?>
 <ol>
      <?php foreach($errors as $key => $value)
      {
         print_error($key, $errors);
       } ?>
  </ol>
<?php
}

simple enough, isn’t it.

simple enough, isn’t it.

Not really. Where does the last php tag close? And which part of my code does this example replace?