Control Structures

Control Structures are a pre-defined statements , which on the basis of some conditions decides which of the group of statements will be executed. We will start with if statement

If

It decides the execution of a statement depending on the condition evaluated in the statement.

	if ($a > $b)
  echo "a is bigger than b";
	
Else

Else statement comes into picture, once the condition given in if statement fails to be true. Here is an example :-

	if ($a > $b) {
  echo "a is greater than b";
   } 
else {
  echo "a is NOT greater than b";
}
	
Else if

This control structure if we have a number of conditions to compare. Ex if we have to compare two numbers, three posiblities are there , so we need else if in this case :-

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

This statement is used if we don't know the exact number of times a code need to be executed. While statement holds a condition and repeats the process as long as the condition passed remains true. As soon as it becomes false, it comes out of loop.

	$pos = 0;
	while($pos!=25){
	  echo "value of $pos is ";
	  echo $pos;
	  $pos++;
	  }  // comes out as soon as $pos==25
	
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. The main difference from regular while loops is that the first iteration of a do-while loop is guaranteed to run (the truth expression is only checked at the end of the iteration), whereas it may not necessarily run with a regular while loop (the truth expression is checked at the beginning of each iteration, if it evaluates to FALSE right from the beginning, the loop execution would end immediately).

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

For statement, perhaps, the most used control structure is used to execute a particular statement a fixed number of time which is known to us.

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

foreach

This is used for handling values of an array one by one. It returns an error if used for any other data type other than array.

$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
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.We will show with an example how a number of if else statements can be easily wrapped using switch statements.

if ($i == 0) {
    echo "i equals 0";
} elseif ($i == 1) {
    echo "i equals 1";
} elseif ($i == 2) {
    echo "i equals 2";
}

switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
}

With this, we would move to functions in PHP.

Member Login

Member Login




Not a Member? Sign Up!




Login to comment

Be the first to comment on this topic







  


<<< Wanna review

Continue >>>