Control Structure in PHP with example


           Control structure pertains to the different methods through which we can control the running flow of the program. These structures take care of a few conditions and make sure that a particular portion of a program is run only under certain conditions or is run at least for a set number of times. 

There is broadly two categories of control structures.
1. If/else condition
2. Loops

Let us talk about If/elseif/else condition. In PHP only one instance of else is allowed although you can have multiple instances of elseif conditions. E.g.

$x = 1;
if ($x < 1){
    echo '$x is less than 1';
}elseif ($x == 1){ // Note the double equals, for comparison
    echo '$x is equal to 1';
}else{
    echo '$x is neither equal to 1 or less than 1';
}

Output : 
$x is equal to 1 (Shown in Image)

Another kind of control statement is Switch statement. This is just like the ‘if statement’ with a difference in the way the condition is checked. Syntax-

switch (expr) {
    case value:
// Do this if value matches
        break;
    case value2:
// Do this if value2 matches
        break;
    default: // [optional]
// Do this if no other cases match. Does not have to be at the end
        break;
}

expr: Is the variable against which the case values will be checked.
Break: It stops a loop from executing and exits. But what is a loop?
Loops are of three types, for while and do while.

1. While:
Syntax-
while (expr) {
// If expr is TRUE, do this, then evaluate expr again
}

A while loop first checks the condition and then enters the loop, therefore there is  chance that the loop doesn’t executes even a single time.

2. Do While
Syntax-
do {
// Do this
} while (expr);

This is just like the while loop, with a little difference. This loop will execute once even if the condition is false because the condition is checked later.
Continue: It breaks the current iteration of the loop and starts from the beginning. 


3. For

Syntax-
for (expr1; expr2; expr3) {
// If expr2 is TRUE, do this

Expr1 is the initialization for any variable which might be used in the loop. Expr2 is the condition, while the expr3 is generally used to manipulate the control variable, or other variables.

4. Foreach

foreach ($array as $value){
// Do something
}
// Another form, for keys and values
foreach ($array as $key => $value){
// Do something
}

For each iteration the loop runs for a fixed number of times. It runs as long as there are elements in the array. You might be confused as to what ‘key=> value’ is, but don’t be. We will learn it in future lectures.
Break[$integer] : We use this to tell the break statement, how many nested loop does it have to break out of.

These control structures are not limited to variables. They can include files too and can perform actions on them. For now all you have to master is the art of getting the maximum functionality by using the minimum number of control structures. Remember even codes do not want to be controlled too much!





Reactions:

0 comments :

Post a Comment