Subscribe To RSS Feeds
Follow Us On Facebook
Suppose when writing a code, you come to a point in the code where you want to execute a same code block over and over and again. So instead of writing that same code block again and again in your script, you can take help of Loops. Loops execute a block of code for a specified number of times, or while a specified condition is true. Each time the code in the loop executes, it is called an iteration. This is useful for many common tasks, such as displaying the results of a query by looping through the returned rows.
PHP Infinite loops are the loops which never ends .The terminating condition in infinte loops always evalutes to true and the loops keeps on executing and it never ends. To explain the concept above let’s look at some examples of infinite loops.
1) Infinte While Loop:
$i=1;
while($i<=10)
{
echo $i;
}
PHP while Loop checks the condition first and if that condition evaluates to true than it executes the block of code which is inside the loop till the condition evaluates to true when condition evaluates to false the control jumps out of the loop and the loop stops.
PHP do … while loop takes an expression such as a while statement but places it at the end. This loop is useful when you want to execute a block of code at least once regardless of the condition value.
PHP for loops provide the same general functionality as while loops, but also provide for a predefined location for initializing and changing a counter value. You can use the PHP FOR LOOP when you know that how many times the code block in a script should run.
PHP FOREACH LOOP is mostly used for looping through arrays. When working with array of data in your code , you may reach to a point in your code where you want to loop through array of data , than you can use PHP FOR EACH LOOP.
When writing a code you may reach to a point in the code where you want to you need to store data for a whole set of information, such as the results of a query than at that situation you can’t you use variables because variables are used to store a single peace of information. When this happens, use PHP ARRAYS. Arrays are a special kind of variable that stores many pieces of data. Arrays allow you to access any of the values stored in them individually yet still copy and manipulate the array as a whole.
Numeric array use number as their indexes. Numeric arrays in PHP can be created by following two ways:
In an associative arrays each key is associated with some value. You can use associative arrays when you want to store data about specific named values, at that point you can use associative arrays which associates every ‘key’ with some ‘value’.
PHP Mutidimentional array is a array in which each element in the main array can be array itself and each element in the subarray can also be an array. To Explain the above concept look at the example below: