LOOP CONCEPT IN PHP
FOR LOOP CONCEPT:
For loop Syntax:
FOR LOOP SYNTAX :
For loop Syntax:
Syntax:
The Syntax Of For Loop Is PHP Is As Follows −
For (Initialization; Test Condition; Iteration Statement){
Statement(S) To Be Executed If Test Condition Is True
}
FOR LOOP SYNTAX :
for (init counter; test counter; increment counter)
{
code to be executed;
}
<?php
for($i=0; $i<5;$i++)
{
echo $i;
}
?>
For loop Example :
<?php
$a=5;
for($i=1;$i<=10;$i++)
{
echo " 5*".$i."==".$a*$i.'<br>';
}
?>
Output:
5*1==5
5*2==10
5*3==15
5*4==20
5*5==25
5*6==30
5*7==35
5*8==40
5*9==45
5*10==50
5*2==10
5*3==15
5*4==20
5*5==25
5*6==30
5*7==35
5*8==40
5*9==45
5*10==50
while loop syntax:
The syntax of while loop in JavaScript is as follows −
while (expression){ Statement(s) to be executed if expression is true }
WHILE LOOP SYNTAX:
The while loop executes a block of code as long as the specified condition is true.
While (condition is true)
{
code to be executed;
}
EXAMPLE :
<?php
$i=0;
while($i<5)
{
echo $i;
$i++;
}
?>
While Loop Example :
<?php
$a=5;
$i=1;
while($i<=10)
{
echo " 5*".$i."==".$a*$i.'<br>';
$i++;
}
?>
output :
5*1==5
5*2==10
5*3==15
5*4==20
5*5==25
5*6==30
5*7==35
5*8==40
5*9==45
5*10==50
5*2==10
5*3==15
5*4==20
5*5==25
5*6==30
5*7==35
5*8==40
5*9==45
5*10==50
The PHP do...while Loop
The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.
Do while syntax:
Syntax
The syntax for do-while loop in JavaScript is as follows −
do{ Statement(s) to be executed; } while (expression);
Note − Don’t miss the semicolon used at the end of the do...while loop.
do
{
code to be executed;
}
while (condition is true);
EXAMPLE :
<?php
$i=0;
do
{
echo $i;
$i++;
}
while($i<5);
?>
Do-while Loop Example :
<?php
$a=5;
$i=1;
do
{
echo " 5*".$i."==".$a*$i.'<br>';
$i++;
}
while($i<=10);
?>
Output :
5*1==5
5*2==10
5*3==15
5*4==20
5*5==25
5*6==30
5*7==35
5*8==40
5*9==45
5*10==50
5*2==10
5*3==15
5*4==20
5*5==25
5*6==30
5*7==35
5*8==40
5*9==45
5*10==50
Simple Application for Practice use of Loop:
(1)write code for table.html file:
<form action=”table.php” method=”post”>
enter your number<input type=”text” name=”number”>
<input type=submit name=submit value=submit>
</form>
(2)write code for table.php file:
<?php
$number=$_POST['number'];
for($i=1;$i<10;$i++)
{
echo $number*$i.'<br>';
}
?>
output:

after click on submit button you will get result.
==============================================================================================================================================
The PHP Foreach Loop
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.
Syntax
foreach ($array as $value) {
code to be executed;
}
Example:
<?php
$abc=array("apple","mango","orange");
foreach($abc as $value)
{
echo "Current Fruits= $value".'<br>';
}
?>
output:
Current Fruits= apple
Current Fruits= mangoCurrent Fruits= orange
Example 2:
<?php
$abc=array("JILL"=>2439,"BOB"=>4986,"SAM"=>45689);
foreach($abc as $key=>$value)
{
echo "key:".$key ;
echo "value:".$value ."<br>";
}
?>
output:
key:JILL value:2439
key:BOB value:4986
key:SAM value:45689
================================================================================================================================================
The each() function returns the current element key and value, and moves the internal pointer forward.
This element key and value is returned in an array with four elements. Two elements (1 and Value) for the element value, and two elements (0 and Key) for the element key.
Syntax
each(array)
| Parameter | Description |
|---|---|
| array | Required. Specifies the array to use |
<?php
$abc=array("JILL"=>2439,"BOB"=>4986,"SAM"=>45689);
while(list($key,$value)=each($abc))
{
echo "key:".$key ;
echo "value:".$value ."<br>";
}
?>
output:
key:JILL value:2439
key:BOB value:4986
key:SAM value:45689
key:BOB value:4986
key:SAM value:45689
==============================================================================================================================================Nested For loop Examples :
<?php
$n=4;
for($i=0;$i<$n;$i++)
{
for($j=0;$j<$i;$j++)
{
echo " 1";
}
echo "<br>";
}
?>
step 1:
i=0 0<4 true
j=0 0<=0 true
print 1
j++
repeat j loop
now j=1
1<=0 false
hence print
new line
step 2:
i=1 1<4 true
j=0 0 <=1 true
print 1
j++
repeat j loop
now j=1
1<=1 true
print 1
j++
repeat j loop
now j=2
2<=1 false
new line
step 3:
i=2 2<4 true
j=0 0<=1 true
print 1
j++
repeat j loop
now j=1
1<=2 true
print 1
j++
repeat j loop
now j=2
2<=2 true
print 1
j++
repeat j loop
now j=3
3<=2 false
new line
=======================================================================
<?php
echo "<h1>Multiplication table</h1>";
echo "<table border=2 width=50%";
for ($i = 1; $i <= 10; $i++ ) { //this is the outer loop ( used for row)
echo "<tr>";
echo "<td>".$i."</td>";
for ( $j = 2; $j <= 10; $j++ ) { // inner loop (used for column)
echo "<td>".$i * $j."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
output:
========================================================================
Example of using the Continue statement :
========================================================================
<?php
echo "<p><b>Example of using the Continue statement:</b><p>";
for ($i=0; $i<=10; $i++) {
if ($i==4){continue;}
echo "The number is ".$i;
echo "<br />";
}
?>
output:
Example of using the Continue statement:
The number is 0
The number is 1
The number is 2
The number is 3
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
The number is 10
========================================================================
Example of using the break statement :
========================================================================
<?php
echo "<p><b>Example of using the break statement:</b><p>";
for ($i=0; $i<=10; $i++) {
if ($i==4){break;}
echo "The number is ".$i;
echo "<br />";
}
?>
output:
Example of using the break statement:
The number is 0
The number is 1
The number is 2
The number is 3
================================================================================================================================================
(1)Find all even numbers between 1 to 100 :
<?php
for ($i=2; $i<=100; $i+=2)
{
echo $i." ";
}
?>
Output :
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42
44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86
88 90 92 94 96 98 100
In the above example
Here loop starts from ($i=2 ) after every count $i increment its value by 2 and Print all even value from( 1 to 100)
(2)Find all odd numbers between 1 to 100 using loop :
<?php
for ($i=1; $i<=99; $i+=2)
{
echo $i." ";
}
?>
output:
1 3 5 7 9 … 99
(3)Find the Sum of even and odd numbers between 1 to 100:
<?php
for ($i=1; $i<=100; $i++)
{
if($i%2==0)
{
@$even=$even+$i;
}
else
{
@$odd=$odd+$i;
}
}
echo "Sum of even numbers=".$even."<br/>";
echo "Sum of odd numbers=".$odd;
?>
Output :
Sum of even numbers=2550
Sum of odd numbers=2500
In the above example
For loop is used because we know how many times loop iterate. inside the for loop we declare if..else condition.
If( $%2==0) condition is true, then code will execute and calculate the sum of even number. otherwise else statement execute and calculate the sum of odd number.
Print the sum of odd ans even number separately.
Add two numbers using loop(Not use + operator):
<?php
$f=$_GET['f'];
$s=$_GET['s'];
for ($i=1; $i<=$s; $i++)
{
$f++;
}
echo "Sum of given numbers=".$f;
?>
<body>
<form>
Enter first number <input type="text" name="f"><br/>
Enter Second number<input type="text" name="s"><br/>
<input type="submit" value="add">
</form>
</body>
Subtract Two Numbers Using Loop(Not Use – Operator).
<?php
$f=$_GET['f'];
$s=$_GET['s'];
for ($i=1; $i<=$s; $i++)
{
$f--;
}
echo "Subtraction of given numbers=".$f;
?>
<html>
<body>
<form>
Enter first number<input type="text" name="f"><br/>
Enter Second number<input type="text" name="s"><br/>
<input type="submit" value="Subtract">
</form>
<body>
Without Using Strrev() Write Code Of Php Using For Loop:
<?php
$string = "sandep";
$len = strlen($string);
for($i=$len; $i > 0; $i--)
{
echo $string[$i-1];
}
?>
Create Table Using For Loop In PHP
<?php
if(isset($_POST['create']))
{
$rows=$_POST['r'];
$cols=$_POST['c'];
echo "<table border='1'>";
for($i=0;$i<$rows;$i++)
{
echo "<tr>";
for($j=0;$j<$cols;$j++)
{
echo "<th>"."r".$i."c".$j."</th>";
}
echo "</tr>";
}
echo "</table>";
}
?>
<html>
<body>
<form method="post">
<table width="400" border="1">
<tr>
<td width="177">Enter number of rows </td>
<td width="207"><input type="text" name="r"/></td>
</tr>
<tr>
<td>Enter number of column </td>
<td><input type="text" name="c"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Create Table" name="create"/>
</td>
</tr>
</table>
</form>
</body>
</html>





Comments
Post a Comment