PHP LOOPS

PHP LOOPS

PHP LOOPS

 

 

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 LOOP

PHP INFINITE LOOPS

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.

Examples Of infinte Loops In PHP

1) Infinte While Loop:

$i=1;

while($i<=10)

{

echo $i;

}



PHP WHILE LOOP

PHP WHILE LOOP

PHP WHILE LOOP

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

PHP DO WHILE LOOP

PHP DO-WHILE LOOP

 

 

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 LOOP

PHP FOR LOOP

PHP FOR LOOP

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 FOR-EACH LOOP

 

 

PHP FOR EACH LOOP

PHP FOREACH LOOP

 

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.



PHP ARRAYS

PHP ARRAYS

PHP ARRAYS

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.



PHP NUMERIC ARRAY

PHP NUMERIC ARRAY

PHP NUMERIC ARRAY

Numeric array use number as their indexes. Numeric arrays in PHP can be created by following two ways:

  • Using Array Identifiers: Array identifiers look like normal variable assignments except a pair of square brackets ([]) are added after the name of the array variable. You can optionally add an index value between the brackets. If you don’t supply an index, PHP automatically picks the lowest empty numeric index value for the array.


PHP ASSOCIATIVE ARRAY

PHP ASSOCIATIVE ARRAY

PHP ASSOCIATIVE ARRAY

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

PHP MUTIDIMENTIONAL ARRAY

PHP MULTIDIMENTIONAL ARRAY

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: