Home / PHP/MySQL Tutorials / Archive by category 'PHP Flow Control'

PHP Flow Control

php-flow-control

Flow control in PHP : Part 3 – Looping constructs FOR LOOP

The for loop is also similar to the while loop, but with the initialization and increment operations embedded within the loops statement.The initialize expression sets the value of the variable to an initial value. This is executed only during the first iteration. The condition checks the value of the variable during each iteration and the code within the loop structure is executed only if the condition is true. The increment expression increments (any other arithmetic operation can be used, increment is only a generalization) the value of the variable during each iteration.

Syntax:
for (initialize; condition; increment)
{
code to be executed;
}

The default part of the code is executed if the variable did not match any of the cases. Break is used to come out of the switch construct on executing the code for a particular case. If break is not used, the remaining cases will also be checked and default case executed before coming out of the switch.

Example code:


for ($count=1; $count<=5; $count++)
{
echo "Count = $count


Flow control in PHP : Part 2 – Looping constructs DO-WHILE

The do-while loop is similar to the while loop except that the condition is checked after executing the statements instead of at the beginning. The block of code within the loop is executed atleast once.

Syntax:
do
{
code to be executed;
}
while (condition);

The default part of the code is executed if the variable did not match any of the cases. Break is used to come out of the switch construct on executing the code for a particular case. If break is not used, the remaining cases will also be checked and default case executed before coming out of the switch.

Example code:

1
2
3
4
5
6
7
8
9
<?php
$i=1;
do
  {
  $i++;
  echo "The number is " . $i . "<br />";
  }
while ($i<=5);
?>

Flow control in PHP : Part 1 – Looping constructs WHILE

The while loop repeats the execution of a set of statements as long as a condition is satisfied.The code within the loop is executed until the condition is TRUE. The condition is checked each time before beginning the execution of the code. Each cycle of executing og the code within the loops is called an iteration.

Syntax:
while (condition)
{
code to be executed;
}

The default part of the code is executed if the variable did not match any of the cases. Break is used to come out of the switch construct on executing the code for a particular case. If break is not used, the remaining cases will also be checked and default case executed before coming out of the switch.

Example code:


$count = 0;
While ($count < 5) {
Echo “Count = $count


Flow control in PHP : Part 4 – Conditional statements SWITCH

The switch statement takes a single variable as input and compares its value with many “cases


Flow control in PHP : Part 3 – Conditional statements IF-ELSEIF-ELSE

This statement is used when more than one condition needs to be evaluated. If the condition following if statement is false, the consition following the elseif statement is checked. The else part is executed only if both the conditions are false.

Syntax:
if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;

Example code:

$num1 = 2;
$num2 = 3;
If( $num1 > $num2) {
Echo “$num1 greater than $num2


Flow control in PHP : Part 2 – Conditional statements IF-ELSE

The if/else statement is used when a set of statements need to be executed if a condition is true, and another set if the condition is false.

Syntax:
If (condition) {
Code to be executed if condition is true;
} else {
Code to be executed if condition is false;
}

Example code:


$num1 = 2;
$num2 = 3;
If( $num1 > $num2) {
Echo $num1 greater than $num2;
$num2 = $num1;
} else {
Echo “Variable num2 might be greater than or equal to variable num1


Flow control in PHP : Part 1 – Conditional statements

Control structures are used to groups the statements into different blocks. grouping code has advantages while developing and grouping is done for the following reasons:

  • when a block of statements needs to be executed based on the outcome of a condition. Conditional statements – if, if…else, if..elseif..else, and switch are used for this purpose.
  • when a block of statements need to be executed more than once. Looping structures – while and for loops are used for this purpose.

IF statement

The if statement is used to execute a set of statements only if a particular condition is true.

syntax:
If (condition) {

Code to be executed if the condition is true

}

Example code:

1
2
3
4
5
6
<?php
$num1 = 5;
$num2 = 3;
If ($num1 > $num2) 
Echo $num1 is greater than $num2
?>

The curly brackets are required only if more than one statements are to be executed.

Example code:


$num1 = 5;
$num2 = 3;
If ($num1 > $num2) {
Echo $num1 is greater than $num2;
Echo “Setting both the numbers equal