4.6. Logiske Kontrolstrukturer

Det følgende eksempel viser syntaxen for de logisk strukturer i PHP.

<pre>
<?php
  echo "If:\n";
   
   $a=1;
   $b=2;

   if ($a<$b)
       {
           echo "$a mindre end $b";
       }
    else
       {
           echo "$a ikke mindre end $b";
       }

  echo "\n\nIf elseif .. else:\n";

   $c="Tux";
   if  
       ($c=="Tux")   {echo "En penguin";}
     elseif
       ($c=="ESR")   {echo "Eric Raymond";}
     elseif
       ($c=="RMS")   {echo "Richard Stallman";}
     elseif
       ($c=="BillG") {echo "Hvem?";}
      else
                     {echo "Ukendt";}


   echo "\n\nSwitch:\n";

   $d=2;

   switch ($d)
    {
     case 0: 
       echo "zero"; break;
     case 1: 
       echo "one";  break;
     case 2:
       echo "two";  break;
     default:
       echo "Unknown";
    }
?>
</pre>

Udskriver:
If:
1 mindre end 2

If elseif .. else:
En penguin

Switch:
two