Sunday 4 December 2016

Chapter 6(PHP) Looping Statements (For Loop,While Loop,Do while Loop )

For Loop In PHP

For Loop is the Loop which runs the code several time in loop
Suppose if we want to print number from 1 to 5 what a person do? he will first declare variable
$a=1;(assign value 1 to variable $a)
$b=2;
$c=3;
and in echo we display one by one $a,$b,$c like this .The number can be displayed with this method but we have to write so many coding lines but with the help of for loop we can write only one coding line and the program will run 5 time automatically  displayed the output .
To better understand this see program below

PROGRAM

<html>
<body>

<?php
for ($i=1; $i<=5; $i++)
  {
  echo "The number is " . $i . "<br />";
  }
?>

</body>
</html>

EXPLANATION

First of all we declare html tag ,body tag and php tag after that we use for loop like 
for ($i=1; $i<=5; $i++) 
in this $i=1; means we declare variable $i and assign value 1 to it.
$i<=5; this means run the program or loop while $i is less than or equal to 5
$i++ this means increment 
and in echo we display $i like
  echo "The number is " . $i . "<br />";
The number is written in" " means it display as it is and we can join the value of $i with the number is like . $i .
dot is used for joining and we use br tag to break the line and to show output like
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5


working 

$i=1 which is less than 5 so condition is true so it will inside coding of for loop and display Si whose value is 1 after that it get incremented from 1 to 2 now value of $i become 2 again it check condition of less than or equal to 5 .again 2 is also smaller than 5 so it goes in inside coding and now value of $i is 2 so in echo it display 2 .so in this way it repeat the code while $i is less than or equal to 5.
so it automatically display number from 1 to 5

OUTPUT


Below is the Example of how to make a table of  of any number

PROGRAM OF TABLE OF 13

<html>
<head>
</head>
<body>


<?php

$number = 13;

for ($x = 1; $x <= 10; $x++)
 {
    echo "$number x $x = ".($number * $x)."<br />";
}

?>

</body>
</html>

OUTPUT



WHILE LOOP

While Loop is also similar to for loop it also runs the block of code as many time as the while condition is true .how to use while use and how it works you better understand with the help of example given below

PROGRAM

<html>
<body>

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

</body>
</html> 

EXPLANATION

First we declare html tag after that body tag and php tag and after php tag we initialize a user define variable  $i and assign a value 1 to it after that  we use while loop like while($i<=5) after that in echo we display or print $i and after that we increment $i and close all the tags .
first the value of $i is 1 which is less than or equal to 5 so condition is true so in echo it will display The number is 1 after that the value of $i is incremented from 1 to 2 and it again it goes in while loop and check the condition now value of $i is 2 it is also less than or equal to 5 so it again runs now it display or print in echo the number is 2 and again it compare and runs while $i is equal to 5.

OUTPUT



DO WHILE LOOP

Do While is little different from while loop. In do while looping statement first the do block will runs after that it will compare with the while loop and runs the code as many time as while loop is true.You will understand better how to use  do while loop with the help of program .The program of do while loop is given below .

PROGRAM

<html>
<body>

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

</body>
</html> 

EXPLANATION


First we declare html tag after that body tag and php tag and after php tag we initialize a user define variable  $i and assign a value 1 to it after that  we use do loop like
do
  {
  $i++;
  echo "The number is " . $i . "<br />";
  }
it do we increment $i means in do loop it increment the value of $i from 1 to 2 and in echo we print or display the incremented value of $i .after that we use while condition like while ($i<=5); after that we close html tag body tag and php tag.
Firstly the value of $i is 1 but as it enter in do it will incremented and become 2 and in echo we will display or print incremented value of $i which is 2 .
But the value of $i for comparing with while loop is 1 because we incremented value of $i in do and when the value of $i is 1 it will display incremented value of $i which is 2 and the loop for $i runs for 5 time and hence it will display 
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6

The number is 2  is displayed when $i is 1
The number is 3  is displayed when $i is 2
The number is 4  is displayed when $i is 3
The number is 5  is displayed when $i is 4
The number is 6  is displayed when $i is 5

OUTPUT





If you any query regarding this than please post a comment below



No comments:

Post a Comment