Skip to main content.
« Variables & Data Types
Functions »

Control Structures

Programming PHP - Chapter 2. Language Basics (pages 46-54)

Overview

Selection Structures

PHP has 3 selection structures:

If-Then-Else

Much like C/Perl. Else-If can be written as either else if or elseif. Curly braces optional (but encouraged).

  1.  <?php
  2.    $age0 = 15;
  3.    $age1 = 19;
  4.    $age2 = 21;
  5.  ?>
  6.  <html>
  7.    <head>
  8.      <title>If-Then-Else</title>
  9.    </head>
  10.    <body>
  11.      <h3>Age <?= $age0 ?></h3>
  12.  <?php
  13.      if($age0 < 18) {
  14.        print "<p>Sorry, nothing for you - go play with your toys</p>";
  15.      }
  16.  ?>
  17.      <h3>Age <?= $age1 ?></h3>
  18.  <?php
  19.     // HTML generation inline
  20.     if($age1 < 18) {
  21.       print "<p>Sorry, nothing for you - go play with your toys</p>";
  22.     elseif ($age1 < 21) {
  23.       print "<p>Smoke</p>";
  24.     else {
  25.       print "<p>Smoke and Drink</p>";
  26.     }
  27.  ?>
  28.  
  29.      <h3>Age <?= $age2 ?></h3>
  30.  <?php  if($age2 < 18) { ?>
  31.      <p>Sorry, nothing for you - go play with your toys</p>
  32.  <?php  elseif ($age2 < 21) { ?>
  33.      <p>Smoke</p>
  34.  <?php  else ?>
  35.      <p>Smoke and Drink</p>
  36.  <?     ?>
  37.    </body>
  38.  </html>
Report a Bug Report a BugDownload DownloadTry It Try It

Ternary Operator

Just like other languages:

  1.  <?php
  2.      $s1 = "A";
  3.      $s2 = "B";
  4.      $s3 = "C";
  5.      $s4 = "D";
  6.      $s5 = "F";
  7.  ?>
  8.  <html>
  9.    <head>
  10.      <title>Ternary Operator</title>
  11.    </head>
  12.    <body>
  13.      <p>s1's grade is <?= (($s1=='A')||($s1=='F'))?"an":"a?> <?= $s1 ?></p>
  14.      <p>s2's grade is <?= (($s2=='A')||($s2=='F'))?"an":"a?> <?= $s2 ?></p>
  15.      <p>s3's grade is <?= (($s3=='A')||($s3=='F'))?"an":"a?> <?= $s3 ?></p>
  16.      <p>s4's grade is <?= (($s4=='A')||($s4=='F'))?"an":"a?> <?= $s4 ?></p>
  17.      <p>s5's grade is <?= (($s5=='A')||($s5=='F'))?"an":"a?> <?= $s5 ?></p>
  18.    </body>
  19.  </html>
Report a Bug Report a BugDownload DownloadTry It Try It

Switch

Can work on various types (String, ints, etc...). Can use a variable as a case label.

  1.  <html>
  2.    <head>
  3.      <title>Switch</title>
  4.    </head>
  5.    <body>
  6.  <?php
  7.      $drink = 21;
  8.      $vote = 18;
  9.  
  10.      // variable to switch on
  11.      $var = 'Rasmus';
  12.      switch ($var) {
  13.          case 'foo':
  14.              print "how boring";
  15.              break;
  16.          case $vote:
  17.              print "go vote";
  18.              break;
  19.          case $drink:
  20.  ?>
  21.  Go drink
  22.  <?
  23.              break;
  24.          case "Rasmus":
  25.              print "Hello, Rasmus";
  26.              break;
  27.          default:
  28.              print "Say what?";
  29.      }   
  30.  ?>
  31.    </body>
  32.  </html>
Report a Bug Report a BugDownload DownloadTry It Try It

Repetition Structures

PHP has 4 repetition structures:

While Loop

PHP's while loop follows normal conventions:

  1.  <html>
  2.    <head>
  3.      <title>While Loop</title>
  4.    </head>
  5.    <body>
  6.  <?php
  7.      $i = 10;
  8.      while($i > 0) {
  9.          echo "<p>"$i"</p>";
  10.          $i--;
  11.      }   
  12.  ?>
  13.  
  14.  <?php 
  15.      $i = 5;
  16.      while ($i > 0) {
  17.      $i--;
  18.  ?>
  19.      <p>Blah</p>
  20.  <?php ?>
  21.    </body>
  22.  </html>
Report a Bug Report a BugDownload DownloadTry It Try It

Do While Loop

Do while is the same as you would expect - don't forget the semicolon.

  1.  <html>
  2.    <head>
  3.      <title>Do While Loop</title>
  4.    </head>
  5.    <body>
  6.  <?php
  7.      $i = 10;
  8.      do {
  9.          echo "<p>"$i"</p>";
  10.          $i--;
  11.      while ($i > 0);
  12.  ?>
  13.  
  14.  <?php 
  15.      $i = 5;
  16.      do {
  17.      $i--;
  18.  ?>
  19.      <p>Blah</p>
  20.  <?php while ($i > 0)?>
  21.    </body>
  22.  </html>
Report a Bug Report a BugDownload DownloadTry It Try It

For Loop

Normal initialize, condition, update format:

  1.  <html>
  2.    <head>
  3.      <title>For Loop</title>
  4.    </head>
  5.    <body>
  6.  <?php
  7.      for($i = 10$i > 0$i--) {
  8.          echo "<p>"$i"</p>";
  9.      }   
  10.  ?>
  11.  
  12.  <?php for($i = 10$i > 0$i--) { ?>
  13.      <p>Blah</p>
  14.  <?php ?>
  15.    </body>
  16.  </html>
Report a Bug Report a BugDownload DownloadTry It Try It

Foreach Loop

PHP has a foreach loop like the other scripting languages we have already looked discussed. Syntax is a bit different - uses a foreach($array as $element) type of notation:

  1.  <?php
  2.      $flintstones = array("Fred""Barney""Wilma""Betty");
  3.      $jetsons = array("George""Elroy""Jane""Judy");
  4.  ?>
  5.  <html>
  6.    <head>
  7.      <title>Foreach Loop</title>
  8.    </head>
  9.    <body>
  10.  <?php
  11.      foreach($flintstones as $person) {
  12.          echo "<p>"$person"</p>";
  13.      }   
  14.  ?>
  15.  
  16.  <?php foreach($jetsons as $person) { ?>
  17.      <p><?= $person ?></p>
  18.  <?php ?>
  19.    </body>
  20.  </html>
Report a Bug Report a BugDownload DownloadTry It Try It

« Variables & Data Types
Functions »