Detri Amelia Chandra
  • HOME
  • PHP Tutorial
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
    • URL Redirect Tool
    • URL Download Encryptor
No Result
View All Result
  • HOME
  • PHP Tutorial
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
    • URL Redirect Tool
    • URL Download Encryptor
Detri Amelia Chandra
No Result
View All Result

9. PHP Loop (While, For, Foreach) Definition and Example

Detri Amelia Chandra by Detri Amelia Chandra
15/01/2020
in PHP, Programming
0
PHP Loop (While, For, Foreach) Definition and Example

PHP Loop (While, For, Foreach) Definition and Example

0
SHARES
267
VIEWS
Share on FacebookShare on Twitter

This time we will discuss looping in php. Iteration in php has its own characteristics. Regularly when you compose code, you need a similar square of code to run again and again a specific number of times. Thus, rather than including a few practically equivalent code-lines in a content, we can utilize circles.
Circles are utilized to execute a similar square of code over and over, up to a specific condition is valid.
In PHP, we have the accompanying circle types:

Recommended Post

Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik

14. PHP Forms – Validation With Required Fields

14. PHP Forms – Validation With Required Fields

  • while – circles through a square of code as long as the predetermined condition is valid
  • do…while – circles through a square of code once, and afterward rehashes the circle as long as the predetermined condition is valid
  • for – circles through a square of code a predetermined number of times
  • foreach – circles through a square of code for every component in an exhibit

The accompanying sections will clarify and give instances of each circle type.

PHP While Loop

The while loop – Loops through a square of code as long as the predetermined condition is valid. See the example below.
The example below displays the numbers from 1 to 5:

1
2
3
4
5
6
7
<?php
$x = 1;
while($x <= 5) {
    echo "The number is: $x <br>";
    $x++;
}
?>

Example Explained

  • $x = 1; – Initialize the loop counter ($x), and set the start value to 1
  • $x <= 5 – Continue the loop as long as $x is less than or equal to 5
  • $x++; – Increase the loop counter value by 1 for each iteration

This example counts to 100 by tens:

1
2
3
4
5
6
7
<?php
$x = 0;
while($x <= 100) {
    echo "The number is: $x <br>";
    $x+=10;
}
?>

The PHP Do While Loop

The do…while loop will consistently execute the square of code once, it will at that point check the condition, and rehash the circle while the predefined condition is valid.
See the example below. The example below first sets a variable $x to 1 ($x = 1). Then, the do while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5:

1
2
3
4
5
6
7
<?php
$x = 1;
do {
    echo "The number is: $x <br>";
    $x++;
} while ($x <= 5);
?>

Note: In a do…while loop the condition is tested AFTER executing the statements within the loop. This means that the do…while loop will execute its statements at least once, even if the condition is false. See example below.

This example sets the $x variable to 6, then it runs the loop, and then the condition is checked:

1
2
3
4
5
6
7
<?php
$x = 6;
do {
    echo "The number is: $x <br>";
    $x++;
} while ($x <= 5);
?>

The PHP For Loop

The PHP for loop is utilized when you know ahead of time how often the content should run.
Parameters:

  • init counter: Initialize the loop counter value
  • test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
  • increment counter: Increases the loop counter value

See the example below. The example below displays the numbers from 0 to 10:

1
2
3
4
5
<?php
for ($x = 0; $x <= 10; $x++) {
    echo "The number is: $x <br>";
}
?>

Example Explained

  • $x = 0; – Initialize the loop counter ($x), and set the start value to 0
  • $x <= 10; – Continue the loop as long as $x is less than or equal to 10
  • $x++ – Increase the loop counter value by 1 for each iteration

See the example below. This example counts to 100 by tens:

1
2
3
4
5
<?php
for ($x = 0; $x <= 100; $x+=10) {
    echo "The number is: $x <br>";
}
?>

The PHP Foreach Loop

The foreach circle works just on exhibits, and is utilized to circle through each key/esteem pair in a cluster.
For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.
See the example below. The following example will output the values of the given array ($colors):

1
2
3
4
5
6
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
  echo "$value <br>";
}
?>

The following example will output both the keys and the values of the given array ($age):

1
2
3
4
5
6
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach($age as $x => $val) {
  echo "$x = $val<br>";
}
?>

 

Tags: PHP
Previous Post

8. PHP Switch Statement

Next Post

10. PHP Function Definition and Example

Related Posts

Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik

Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik

30/04/2020
PHP Forms - Validation With Required Fields

14. PHP Forms – Validation With Required Fields

15/02/2020
PHP Forms - Validation With Required Fields

14. PHP Forms – Validation With Required Fields

15/02/2020
PHP Form Handling

13. New Performing PHP Form Handling

13/02/2020
Learn PHP Global Variables - Superglobals With Example

12. Learn PHP Global Variables – Superglobals With Example

20/01/2020
PHP Array Example, Complete Editions

11. PHP Array Example, Complete Editions

17/01/2020
Next Post
PHP Function Definition and Example

10. PHP Function Definition and Example

Tinggalkan Balasan Batalkan balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

Top Stories

Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik

Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik

30/04/2020
PEMROGRAMAN WEB DAN PERANGKAT BERGERAK – PERPUSTAKAAN PART-1

Pemrograman Web dan Perangkat Bergerak – Perpustakaan Part-1

16/04/2020
PEMROGRAMAN WEB DAN PERANGKAT BERGERAK – PERPUSTAKAAN PART-1

Pemrograman Web dan Perangkat Bergerak – Perpustakaan Part-1

16/04/2020

News & More

Categories

  • Counseling
  • Consultations
  • Relationship
  • Friendship
  • Singlehood

About Us

We bring you the best Premium WordPress Themes that perfect for news, magazine, personal blog, etc. Check our landing page for details.

Connect on Social

© 2019 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • HOME
  • PHP Tutorial
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
    • URL Redirect Tool
    • URL Download Encryptor

© 2021 Detri Amelia Chandra - IT Tips and Programming Tutorial.