Sunday, March 22, 2009

Control Structures in php

Php uses various control structures. The control structure used in php along with some examples are noted below.

1)if : If the expression specified within "if statement" evaluates to TRUE then PHP will execute statement, and if it evaluates to FALSE if block is ignored.
A simple example,

<?php
if ($a > $b)
echo "The value of a variable is bigger than the value of b variable";
?>

2)else : If the condition inside if statement is not met then else block is executed.

<?php
if ($a > $b) {
echo "A value is greater than B value.";
} else {
echo "A value is not greater than B value.";
}
?>

3)elseif/else if: "elseif" / "else if" extends the if statement to execute a different statement in case the original if expression evaluates to FALSE.
The following is an example:

<?php
if ($x > $y) {
echo "x is bigger than y";
} elseif ($x == $y) {
echo "x is equal to y";
} else {
echo "x is smaller than y";
}
?>

Note that elseif /else if is same if brace is used. The only difference is "else if" can't be used if colon is used. Below is an example that will show the difference between elseif and elseif.

<?php
//This one is incorrect
$x=3;
$y=2;
if($x>$y): echo "x is greater than y";
else if ($x==$y): echo "both are equal";
else: echo "y is greater than x";
endif;
?>

Parse error: parse error, expecting `':'' in C:\xampp\htdocs\Elseif.php on line 6

<?php
//This one is correct
$x=3;
$y=2;
if($x>$y): echo "x is greater than y";
elseif ($x==$y): echo "both are equal";
else: echo "y is greater than x";
endif;
?>

x is greater than y

4)while: As long as the while expression evaluates to true, the statement inside while statement executes. Below is an example.

<?php
$a=10;
while ($a<15){
echo $a;
$a++;
}
?>

This will print 1011121314 in the browser.

5)Do .. while: Almost same as while loop but it ensures that at least one time the statement will be executed as the checking is done at the end of the iteration.

Below is an example.

<?php
$a=10;
do{
echo $a;
}while ($a>14);
?>

It will print 10 in the browser.

6)for : The syntax of for loop is,
for (expr1; expr2; expr3)
statement

The first expression (expr1) is executed once at the beginning of the loop.

In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues. Loop terminate whenever this expression evaluates to FALSE. If multiple expression is given in expr2 checking is done by AND operation between them.

At the end of each iteration, expr3 is executed.

Below is an example of for loop which will print 20 in the browser.

<?php
for ($a=20,$b=5;$a>10, $b<6;$a++, $b++)
echo $a;
?<


7)foreach: foreach construct is specially made to handle arrays. For normal variables it can't be used. When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array.
The following example will produced output 1234.

<?php
$numarr=array(1,2,3,4);
foreach ($numarr as $value){
echo $value;
}
?>

8)break: break ends execution of the current for, foreach, while, do-while or switch structure. With break you can supply a numeric number which tells how many structure it will end. For example,
while(){
switch(){
case 1:
break 2;
case 2:
break 1;
}
}
In this case break 2 will exit both the switch and while.
break 1 will only exit switch.

9)continue: Continue is used within looping structures and do following.
-Skip the rest of the current loop iteration.
-Evaluates the condition.
-Increase the iteration number by 1.

As of switch statement, continue statement also take numeric number.

10)switch: The switch statement is similar to a series of IF statements but constructs is different.
Below is an example of switch .. case which will display a is 3 on the browser.

<?php
$a=3;
switch($a){
case 1:
echo "a is 1";
break;

case 2:
echo "a is 2";
break;

case 3:
echo "a is 3";
break;

default:
echo "a is neither 1 nor 2 nor 3";
}
?>

11)return: If return is used inside function, statement immediately ends execution of the current function, and returns its argument as the value of the function call. Note that return is not a function. If you use return($a) then value of variable a is returned.

12)goto: The goto operator can be used to jump to other instruction in the program. --The target place is specified by the label and a colon.
-goto is followed by that label.
Below is an example which will print "Goto statement test" in the browser.

<?php
goto l;
echo "This will not execute";

l:
echo "Goto statement test";
?>

Note that the goto operator is available since PHP 5.3.

No comments:

Post a Comment