Sunday, February 4, 2018

PHP FOR LOOPS

                                       PHP LOOPING

We control the statement in two different ways

                                           1.Branching
                                           2.Looping

Looping statements in PHP are used to execute the same block of code a specified number of times.

Looping:

         Very often when you write code, you want the block of code to run a number of times.You can use looping statements in your code to perform this.

Not only PHP, In programming  we have the following looping statements:

   1.While loop:

             In while,  loops through the block of code if and as long as a specified condition is true.

   2.Do...while :

              loops through a block of code once, and then repeats the loop as long as a special condition is true.

   3.For:
                   
           In for, loops through a block of code a specified number of times.

  4.For each:
      
             loops through a block of code for each element in an array.


The while statement:


              The while statement executes a block of code if and as long as a condition is true.

Syntax:

        while(condition)
        code to be executed;

Example:

        The following example will explain the while loop conditions.

<html>
<body>
<?php
$i=1;
while($i < =6)
{
echo "The number is"  . $i  .  "<br/>";
$i++;
}
?>

</body>
</html>

Output:

The numer is: 1
The numer is: 2
The numer is: 3
The numer is: 4
The numer is: 5
The numer is: 6

Do..while statement:

                 The do while statement will execute a block of code at least once,it will repeat the loop as long as a condition is true.

  syntax:

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



<html>
<body>
<?php
$i=0;
do
{
$i++;
 echo "The number is" . $i . "<br />";
}
while ($ i < 5);
?>
</body>
</html>



Output:

The numer is : 1
The numer is : 2
The numer is : 3
The numer is : 4
The numer is : 5


The for statment:


<html>
<body>
<?php
for($x=0;$x <=5; $++)
echo "The number is: $x <br>";
}
?>


</body>
</html>


Output:

The number is:0
The number is:1
The number is:2
The number is:3
The number is:4
The number is:5

No comments:

Post a Comment

A R Murugadoss --- Film Career and Biography

         A R Murugadoss --- Film Career a Biography  Early Life :          A.R Murugadoss was Born on 25th September in the Year o...