Switch with fall through in PHP

Description

The following code shows how to switch with fall through.

Example


<!DOCTYPE html>/*from www  .  j a v a  2 s. co m*/
<html>
  <body>
    <table border="1">
      <tr>
        <th>Number</th>
        <th>Odd or Even?</th>
        <th>Prime?</th>
      </tr>
<?php

for ( $i = 1; $i <= 10; $i++ ) {
  $oddEven = ( $i % 2 == 0 ) ? "Even" : "Odd";
  switch ( $i ) {
    case 2:
    case 3:
    case 5:
    case 7:
      $prime = "Yes";
      break;
    default:
      $prime = "No";
      break;
  }
?>
      <tr>
        <td><?php echo $i?></td>
        <td><?php echo $oddEven?></td>
        <td><?php echo $prime?></td>
      </tr>
<?php
}
?>
    </table>
  </body>
</html>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition