PHP While, Do-While, For and Foreach Loops

Different Types of Loops in PHP

Loops area unit wont to execute identical block of code once more and once more, till a definite condition is met. the essential plan behind a loop is to change the repetitive tasks at intervals a program to save lots of the time and energy. PHP supports four differing types of loops.

While Loops: Through a block of code till the condition is judge to true.

Do while: The block of code dead once then condition is evaluated. If the condition is true the statement is perennial as long because the such condition is true.

For  Loops :Through a block of code till the counter reaches a such range.

Foreach loops: through a block of code for every part in associate array.

You will conjointly learn the way to loop through the values of array exploitation foreach() loop at the top of this chapter. The foreach() loop work specifically with arrays.

You also can read this below article which is very useful for PHP Developer


Function Use in PHP

How to Define Array in PHP

 How to Define Varable in PHP

PHP whereas Loop

The whereas statement can loops through a block of code till the condition within the whereas statement judge to true.

while(condition){
// Code to be dead
}
The example below outline a loop that starts with $i=1. The loop can still run as long as $i is a smaller amount than or capable three. The $i can increase by one anytime the loop runs:

ExampleRun this code »
<?php
$i = 1;
while($i <= 3){
$i++;
echo "The range is " . $i . "
";
}
?>

PHP do while Loop

The do-while loop may be a variant of whereas loop, that evaluates the condition at the top of every loop iteration. With a do-while loop the block of code dead once, then the condition is evaluated, if the condition is true, the statement is perennial as long because the such condition evaluated to is true.

do{
// Code to be dead
}
while(condition);
The following example outline a loop that starts with $i=1. it'll then increase $i with one, and print the output. Then the condition is evaluated, and also the loop can still run as long as $i is a smaller amount than, or capable three.

ExampleRun this code »
<?php
$i = 1;
do{
$i++;
echo "The range is " . $i . "
";
}
while($i <= 3);
?>

Difference Between whereas and do…while Loop

The whereas loop differs from the do-while loop in one vital approach — with a jiffy loop, the condition to be evaluated is tested at the start of every loop iteration, thus if the conditional expression evaluates to false, the loop can ne'er be dead.

With a do-while loop, on the opposite hand, the loop can continually be dead once, albeit the conditional expression is fake, as a result of the condition is evaluated at the top of the loop iteration instead of the start.

PHP for Loop

The for loop repeats a block of code till a definite condition is met. it's usually wont to execute a block of code sure range of times.

for(initialization; condition; increment){
// Code to be dead
}
The parameters of for loop have following meanings:

Initialization — it's wont to initialize the counter variables, and evaluated once flatly before the primary execution of the body of the loop.
condition — within the starting of every iteration, condition is evaluated. If it evaluates to true, the loop continues and also the nested statements area unit dead. If it evaluates to false, the execution of the loop ends.
increment — it updates the loop counter with a replacement worth. it's judge at the top of every iteration.
The example below defines a loop that starts with $i=1. The loop can continuing till $i is a smaller amount than, or capable three. The variable $i can increase by one anytime the loop runs:

ExampleRun this code »
<?php
for($i=1; $i<=3; $i++){
echo "The range is " . $i . "
";
}
?>

PHP foreach Loop

The foreach loop is employed to reiterate over arrays.

foreach($array as $value){
// Code to be dead
}
The following example demonstrates a loop which will print the values of the given array:

ExampleRun this code »
<?php
$colors = array("Red", "Green", "Blue");

// Loop through colours array
foreach($colors as $value)";
">
?>

There is an extra syntax of foreach loop, that is extension of the primary.

foreach($array as $key => $value){
// Code to be dead
}
ExampleRun this code »
<?php
$superhero = array(
"name" => "Peter Parker",
"email" => "peterparker@mail.com",
"age" => eighteen
);

// Loop through superhero array
foreach($superhero as $key => $value)";
">
?>
Faridabad Ford | Best Ford Showroom and Dealership in Faridabad

Welcome to Faridabad Ford, the largest & best ford showroom and dealership in Faridabad, your Biggest Ford Dealer for New and Used Car Sales, Servicing, Parts and Accessories.

Ghaziabad Ford | Best Ford Showroom and Dealership in Ghaziabad

Welcome to Ghaziabad Ford, the largest & best ford showroom and dealership in Ghaziabad, your Biggest Ford Dealer for New and Used Car Sales, Servicing, Parts and Accessories. We can help make your process of Buying, Servicing or Selling your car a lot easier.

List of Backlinks
http://www.faridabadford.com/
http://www.ghaziabadford.com/

List of Backlinks

https://twitter.com/FordNoida

https://www.scoop.it/u/noida-ford

https://in.pinterest.com/pin/737042295250205010

https://www.tumblr.com/blog/noidaford

https://www.reddit.com/user/Ghazibadford/comments/cr4zgt/noida_ford_showroom/

https://www.reddit.com/user/Ghazibadford/comments/cr41d6/ghaziabad_ford/

https://www.tumblr.com/blog/ghaziabadford

https://in.pinterest.com/pin/770748923710938964

https://www.scoop.it/u/ghaziabad-ford

https://twitter.com/GhaziabadF

https://www.reddit.com/user/Faridabadford/comments/cr35gf/faridabad_ford/

https://www.tumblr.com/dashboard

https://in.pinterest.com/delhifords/

https://www.scoop.it/u/faridabad-ford

https://twitter.com/FaridabadF


http://songvault.fm/account.htm

http://bbs.vernee.cc/home.php?mod=space&uid=919925

https://www.informationweek.com/profile.asp

https://onmogul.com/metro-chevrolet

http://www.effecthub.com/user/1672537

http://songvault.fm/account.htm

https://www.informationweek.com/profile.asp

https://onmogul.com/delhi-ford

http://www.effecthub.com/user/1672550

http://songvault.fm/account.htm

http://bbs.vernee.cc/home.php?mod=space&uid=919865

https://www.informationweek.com/profile.asp

https://onmogul.com/hyundai

http://www.effecthub.com/user/1672521

Comments

Popular posts from this blog

How Outsourced Healthcare Services Leads to Improved Patient Experience?

BS6 compliant Ford EcoSport launched in India, price starts at Rs 8.04 lakhs

Best DoFollow profile creation sites list OCT 2019