for loop in PHP
The for loop is used when you know in advance how many times the script should run.Syntax
1
2
3
4
|
for (initialization; condition; increment)
{
code to be executed;
}
|
Parameters of for loop :
Initialization :is used to set a counter.
Condition : if condition is true loop will continue, if the condition is false loop ends.
Increment : used to increment the counter.
Eg i (print the statement 5 times)
1
2
3
4
5
6
|
<?php
for ($i=1; $i<=5; $i++)
{
echo "The Number is: ".$i."<br/>";
}
?>
|
Output
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
We want to print the statement 5 times. we initialize how many times loop should iterate.
loop starts from ($i=1)and ends($i<=5) so output displays five times defined statement.
Write a program to print your name 10 times
1
2
3
4
5
6
7
|
<?php
$name="rexx";
for ($i=1; $i<=5; $i++)
{
echo "My Name is: ".$name."<br/>";
}
?>
|
Output
My Name is rexx
My Name is rexx
My Name is rexx
My Name is rexx
My Name is rexx
here output displays five times defined statement(My Name is ) with a variable(rexx)
Find the sum of 1 to 100.
1
2
3
4
5
6
7
8
9
|
<?php
$sum=0;
for ($i=1; $i<=100; $i++)
{
$sum=$sum+$i;
}
echo $sum;
?>
|
Output
5050
Variable( $sum ) holds value(0). For( ) loop is used to print sum of numbers.
set loop iteration, loop will continue to run as long as ( $i<=100) . So output display 5050
Find all even numbers between 1 to 100
1
2
3
4
5
6
7
8
9
|
<?php
$sum=0;
for ($i=1; $i<=100; $i++)
{
$sum=$sum+$i;
}
echo $sum;
?>
|
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
Here loop starts from ($i=2 ) after every count $i increment its value by 2 and Print all even value from( 1 to 100)
Find all odd numbers between 1 to 100 using loop.
1
2
3
4
5
6
7
|
<?php
for ($i=2; $i<=100; $i+=2)
{
echo $i." ";
}
?>
|
Output
1 3 5 7 9 … 99
Loop start from($i=1) to ($i<=99) every time $i increment its value by 2. if the value of ($i=1) then it will become 3. therefore all odd value are print.
Find the Sum of even and odd numbers between 1 to 100.
1
2
3
4
5
6
7
8
|
<?php
for ($i=1; $i<=99; $i+=2)
{
echo $i." ";
}
?>
|
Output
Sum of even numbers=2550
Sum of odd numbers=2500
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).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?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 given numbers=1000
Enter first number
Enter Second number
First we create a HTML script to take input from users.
when a value entered by user and click on button ,value redirected to PHP script page.$_GET[ ] is used to collect the value that is entered by user.
Now we check the sum. but sum is generate using (+) operator.
iteration start from ($i=1 to $i<=$s) it means loop depend on value of second textbox. Value entered by user in first textbox is 500 Value entered by user in second textbox is 500 output become: 1000. because value of first textbox is incremented. for loop will continue to run as long as ( $i<=$s ) condition is true.
Subtract two numbers using loop(Not use – operator).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?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>
|
Output
Subtraction of given numbers=500
Enter first number
Enter Second number
Nested Loop
Print the following Pattern1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php
for ($i=1; $i<=5; $i++)
{
for($j=1;$j<=$i;$j++)
{
echo $j." ";
}
echo "<br/>";
}
?>
|
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Print the following Pattern
** *
* * *
* * * *
* * * * *
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?php
for ($i=1; $i<=5; $i++)
{
for ($k=5; $k>$i; $k--)
{
//print one space throgh html ;
echo " ";
}
for($j=1;$j<=$i;$j++)
{
echo "*";
}
echo "<br/>";
}
?>
|
Output
*
* *
* * *
* * * *
* * * * *
Create table using for loop in PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<?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>
|
No comments:
Post a Comment