Control Structures in PHP


Control Structures

Control Structures:

In PHP, there are two primary types of Control Structures: Conditional Statements and ControlLoops. Different types are..

1.if

2.else

3.elseif/else if

Alternative syntax for control structures

4.while

5.do-while

6.for

7.foreach

8.break

9.switch

10.include

11.include_once

1.if:

Syntax:

if (expr)

 statement

Ex:

<?php //IF loop

$a=30;$b=20;

if ($a > $b)

 echo "a is bigger than b";

?>

output: a is bigger than b

2.else:

<?php

$a=20;$b=30;
if ($a $b) {
  echo "a is greater than b";
} else {
  echo "a is NOT greater than b";
}
?>

output: a is  NOT greater than b

3.elseif/else if:

<?php $a=20;$b=30;
if ($a $b) {
    echo "a is bigger than b";
} elseif ($a == $b) {
    echo "a is equal to b";
} else {
    echo "a is smaller than b";
}
?>

output: a is smaller than b

4.While Statement:

Syntax:

while (expr)

   statement

or by using the alternate syntax:

while (expr):

   statement

   ...

endwhile;

Example:

<?php

/* example 1 */

$i = 1;

while ($i <= 10) {

   echo $i++;  /* the printed value would be

                  $i before the increment

                  (post-increment) */

//echo "<br>";

}output: 12345678910

/* example 2 */

$i = 1;

while ($i <= 10):

   echo $i;

   $i++;

endwhile;?>

 

output: 12345678910

5.do-while:

<?php
$i 1;
do {
    echo $i;
} while ($i 0);
?>

output: 0

The above loop would run one time exactly, since after the first iteration, when truth expression is checked, it evaluates to FALSE ($i is not bigger than 0) and the loop execution ends.

6.For Loop:

for (expr1; expr2; expr3)

   statement

Example:

<?php

/* example 1  FOR LOOP*/

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

   echo $i;

}?>

output:: 12345678910

7. ForEach:

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes:

foreach (array_expression as $value)

   statement

foreach (array_expression as $key => $value)

   statement

The first form loops over the array given by array_expression. On each iteration, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next iteration, you'll be looking at the next element).

The second form will additionally assign the current element's key to the $key variable on each iteration.

Example:

<?php

$arr = array(1, 2, 3, 4);

foreach ($arr as $value) {

   $value = $value * 2;

echo $value;

}

foreach ($arr as $key => $value) {

   // $arr[3] will be updated with each value from $arr...

   echo "{$key} => {$value} ";

   //print_r($arr);

}

?>

Output: 2 4 6 8
0 => 1 1 => 2 2 => 3 3 => 4

8.Break:

break ends execution of the current forforeachwhiledo-while or switch structure.

Example:

<?php 
echo "hello";
if (true) break;
echo " world"
?>

output:hello

Fatal error: Cannot break/continue 1 level in C:\wamp\www\webpage\test1.php on line 93

9. 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.
Example #1 switch structure:

<?php

$i=1;

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;} ?>

OUTPUT: i equals 1 i equals 1

10. Include:

1.The include statement includes and evaluates the specified file.

2.Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_pathinclude will finally check in the calling script's own directory and the current working directory before failing. The include construct will emit a warning if it cannot find a file; 

Ex:

<?php

$color = 'green';

$fruit = 'apple';

//echo "A $color $fruit"; // A

include 'test2.php';

echo "A $color $fruit"; // A green apple ?>

Output: 5985int(5985) A green apple

11.Include_once:

 The include_once statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include statement, with the only difference being that if the code from a file has already been included, it will not be included again, and include_once returns TRUE. As the name suggests, the file will be included just once.

 include_once may be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, so in this case it may help avoid problems such as function redefinitions, variable value reassignments, etc.

<?php

include_once "test.php"; // this will include a.php

include_once "TEST.php"; // this will include a.php again! (PHP 4 only)

?>

   Output:Wont Come.. becoz include_once is used for only one time included.

Custom Search

%d bloggers like this: