Suppose we have a form and we want from user to select from a list of check boxes and check his favorite choices, post that data as an array and we want to display that data by calling array and arranging it in a loop. Then for this purpose consider following example.
(more…)
PHP ForEach Loop
PHP provides an easy way to use every element of an array with the For each statement. For each executes the code at each item in the specified array. While a For Loop and While Loop will continue until some condition fails, the For Each loop will continue until it has gone through every item in the array.
We have an associative array that stores the names of people in a company as the keys with the values being their age. We want to know how old everyone is at work so we use a Foreach loop to print out everyone’s name and age.
(more…)
PHP Do While Loop
A “do while” loop is a slightly modified version of the while loop. A do-while loop first do’s and secondly checks the while condition!
PHP Do While Loop Structure
- Define
- Do
- Print
- Increment or decrement
- Condition
(more…)
PHP While Loop
The while keyword takes a condition in parentheses, and the code block that follows is repeated while that condition is true. If the condition is false initially, the code block will not be repeated at all.
The repeating code must perform some action that affects the condition in such a way that the loop condition will eventually no longer be met; otherwise, the loop will repeat forever. Such loops are Infinite Loops.
(more…)
PHP For Loop
For loop consists of three phases. i.e.
- Define
- Condition
- Increment or decrement
First we define something. Then we apply a condition and then under that condition we increment or decrement that defined thing.
In complex way;
- The first part is an expression that is evaluated once when the loop begins.
- The second part is the condition. While the condition is true, the loop continues repeating. If the condition is false to start with, the following code block is not executed at all.
- The third part is an expression that is evaluated once at the end of each pass of the loop.
(more…)