iklan

✔ How Exactly Php While, Do-While, For And Foreach Loops Works??

How to use While, Do-While, For and Foreach Loops in PHP? Loops in PHP are used to execute the same block of code a specified number of times. PHP supports following four loop types.
  1. for − loops through a block of code a specified number of times.
  2. while − loops through a block of code if and as long as a specified condition is true.
  3. do...while − loops through a block of code once, and then repeats the loop as long as a special condition is true.
  4. foreach − loops through a block of code for each element in an array.

Video Tutorial PHP While, Do-While, For and Foreach Loops


The for loop statement

The for statement is used when you know how many times you want to execute a statement or a block of statements.

The example for loop statatement

  <?php   for ($i=0; $i <5 ; $i++) {     echo "Display \$i Variable " .$i. "<br>";   }   ?> 

This for statement results is
Display $i Variable 0
Display $i Variable 1
Display $i Variable 2
Display $i Variable 3
Display $i Variable 4

The while loop statement

The while statement will execute a block of code if and as long as a test expression is true.

If the test expression is true then the code block will be executed. After the code has executed the test expression will again be evaluated and the loop will continue until the test expression is found to be false.

The example while loop statatement

  <?php   $x=0;   while ($x < 5) {     echo "Display \$x Variable " .$x. "<br>";     $x++;   }   ?> 

This while statement results is
Display $i Variable 0
Display $i Variable 1
Display $i Variable 2
Display $i Variable 3
Display $i Variable 4

The do...while loop statement

The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true.

The example do...while loop statatement

  <?php   $x=0;   do {     echo "Display \$x Variable " .$x. "<br>";     $x++;   } while ($x < 5);   ?> 

This do..while statement results is
Display $i Variable 0
Display $i Variable 1
Display $i Variable 2
Display $i Variable 3
Display $i Variable 4

The foreach loop statement

The foreach statement is used to loop through arrays. For each pass the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass next element will be processed.

The example foreach loop statatement

  <?php   $x = [0,1,2,3,4];   foreach ($x as $key => $value) {     echo "Display \$x Variable " .$value. "<br>";   }   ?> 

This foreach statement results is
Display $i Variable 0
Display $i Variable 1
Display $i Variable 2
Display $i Variable 3
Display $i Variable 4
Follow us on youtube for more awesome videos and see you next lessons ..

Sumber http://scqq.blogspot.com

Berlangganan update artikel terbaru via email:

0 Response to "✔ How Exactly Php While, Do-While, For And Foreach Loops Works??"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel