Home / code / php

Controle structure in PHP

 

if

The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of C.
If you don't need the else-part, you can exclude it.

if (expr)
{
  statement
}
else
{
  statement
}
Example:
if ($a > $b)
{
  print "a is bigger than b";
}
else
{
  print "a is not bigger than b";
}

Short if

Short version of if.
$c will be true if $a is bigger the 2.

$c = ($a > 2 ? true : false)
Another example:
$c = ($a==2) ? 'Yes' : 'No';

elseif

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

if ($a > $b)
{
  print "a is bigger than b";
}
elseif ($a == $b)
{
  print "a is equal to b";
}
else
{
  print "a is smaller than b";
}

while

A while loop is a loop, it loops as long as the expression is true.

while (expr)
{
  statement
}
Example:
$i = 1;
while ($i <= 10)
{
  print $i++;  /* the printed value would be
                $i before the increment
                (post-increment) */
}

do ... while

do..while loops are very similar to while loops, except the truth expression is checked at the end of each iteration instead of in the beginning. Which means that the loop runs at least once.

$i = 0;
do
{
    print $i;
} while ($i>0);

for

You use for-loops when you know how many times you need to loop.

for ($i = 1; $i <= 10; $i++)
{
  print $i;
}

foreach

PHP 4 (not PHP 3) includes a foreach construct, much like Perl and some other languages. This simply gives an easy way to iterate over arrays. There are two syntaxes; the second is a minor but useful extension of the first:

foreach(array_expression as $value)
{
  statement
}

foreach(array_expression as $key => $value)
{
  statement
}
Example:
foreach ($arr as $value)
{
  echo "Value: $value<br>\n";
}

foreach ($arr as $key => $value)
{
  echo "Key: $key; Value: $value<br>\n";
}

Switch

The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.

switch ($i)
{
  case 0:
	print "i equals 0";
	break;
  case 1:
	print "i equals 1";
	break;
  case 2:
	print "i equals 2";
	break;
  default:
	print "i is not equal to 0, 1 or 2";
}

Break

break ends execution of the current for, foreach while, do..while or switch structure.

break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.

$i = 0;
while (++$i)
{
  switch ($i)
  {
    case 5:
	echo "At 5<br>\n";
	break 1;  /* Exit only the switch. */
    case 10:
	echo "At 10; quitting<br>\n";
	break 2;  /* Exit the switch and the while. */
    default:
	break;
  }
}

Continue

continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the beginning of the next iteration.

continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of.

$i = 0;
while ($i++ < 5)
{
  echo "Outer<br>\n";
  while (1)
  {
	echo "  Middle<br>\n";
	while (1)
	{
	  echo "  Inner<br>\n";
	  continue 3;
	}
	echo "This never gets output.<br>\n";
  }
  echo "Neither does this.<br>\n";
}

 

TOP

Latest script:

 

Books: