foreach with empty data set

cakephp, foreach, php

Solution

Check whether it is empty or not:

if ( !empty( $person ) ) {
  foreach ( $person as $cast ) {
    echo "<li>$cast</li>";
  }
} else {
  echo "<li>This play has no cast members.</li>";
}

Problem

I am trying to use a foreach statement to display a list ul>li. In case the list has no elements how can i handle that criteria. ``` <ul> <?php foreach($person['CastsMovie'] as $cast): ?> <li><?php echo $this->Html->link($cast['Movie']['name'], array('controller' => 'movies', 'action' => 'view', $cast['movie_id'], 'admin' => true), array('escape' => false)); ?> (<?php echo date("Y", strtotime($cast['Movie']['MovieDetail']['release_date'])); ?>)</li> <?php endforeach; ?> </ul> ```

Original source