PHP Control Structures

If statements

 
if ($country == "ca") {
    echo("Canada");
}

if (4 == 3) 
    echo("Never happen");

if (4 > 3) {
    $a = 1;
}
else {
     $a = 2;
}

if ($a == 2) {
     $a = 3;

elseif ($a == 3) {
     $a = 5;
}

<?php if ($a == 4): ?>
    <table border=1>
<?php elseif ($a = 6): ?>
    <table border=2>
<?php else: ?>
    $a = 8;
<?php endif; ?>

Switch Statement

switch ($a) {
    case 1:
       echo("One");
       break;
    case $a: // not allowed in C, C++, or Java
       echo("Two");
       break;
    default:
       echo("None");
}

While Loop

$a = 3;
while ($a--) {
    echo("again!!");
}

<?php while ($a > 3): ?>
    <img src=fred.jpg>
<?php endwhile; ?>

For Loops

for($i = 0; test($i); $i++) {
    echo($i);
}

<?php for($i =0; test($i); $i++): ?>
   <img src=icon.jpg>
<?php endfor; ?>

Require Files

Require files are included into the main file at compile time.  These are the same as "include" files in C++ or "import" files in Java.
require ("common.php");

Include Files

Include files differ from require files in that the include file is executed in place of the include line.  That's different than the require file, and the code below would be different if you executed it with include or require.
for($i = 0; $i < 10; $i++) {
    include("imagefile" . $i . " .php");
}