Loops in PHP are used to execute the block of code specified number of times. If you have a need to print tables 1 to 20 then you have specified loop initiate from 1 to 20. This condition will be specified in loops.
Loops are the methods of PHP, which used to set the compiler work limit of the number. The compiler checks the condition and executes the code if the limit does not end. If the limit is ended, the compiler gets exit from the code of block (Exit from the loop)
Types of Loop
Loop through the block of code specified number of time execute.
Loops depend on the condition if and as long as a specified condition is true.
First, execute the code ,and then repeats the loop as long
as a specified condition is true.(first check the statements then go to the condition ).
To perform an action for each value in an Array .Use values different-different way in time .
For loop in PHP :-
In for loop you have to specify initial value and condition. Block of code will execute until the condition is true when the condition is false compiler will be the exit of the block .
for (initialization; condition; increment)
code to be executed;
- Where the value begins.(ex. $i=0)
- Specify condition here . (ex. $i<20)
- Value of $i will be increment until the condition is true.
Examples of For Loop
In the following example create a table using for loop .
Print a table
for( $i=0; $i<20; $i++ )
$i=0; -- Initial Value of $i(variable) is zero .
$i<20; -- Value of $i lessthan 20 .
$i++ -- Value will be increment .
Array with For Loop
In the earlier tutorials, we have discussed Arrays . Now we will add A -Z letters inside the array with the help of for Loop. If you add A -Z letters in the array by yourself, then it takes a lot of time and there is a lot of complexity in adding letters inside the array. But with the help of For Loop, you can easily store A TO Z Letters.
We use the array_push() function to add elements on to the end array. Let's create an empty array using array() function and add elements through the for loop and array_push function.
$letters = array();
for ($i= 'A'; $i != 'AA'; $i++)
echo print_r($letters, true);
 => A
 => B
 => C
 => D
 => E
 => F
 => G
 => H
 => I
 => J
 => K
 => L
 => M
 => N
 => O
 => P
 => Q
 => R
 => S
 => T
 => U
 => V
 => W
 => X
 => Y
 => Z
In this above example, we created an array. After that, we made the array variable. Inside the for loop, we created a different variable and initialized from A.After that a condition is created and the variables are incremented. These are the most important fact of for loop that you create a condition for compiler works limit.
In the array_push () function we have passed 2 parameters. The first parameter has an array variable and the second parameter has another for loop variable. We used print_r() function to display the array standard formate.
Integrate Payumoney payment gateway in PHP website | Source Scripts
Dynamic Countdown timer using JS ,PHP and MYSQL | Animated timer
Complete Blog CMS system in PHP with MYSQL database | PHP scripts