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

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

15/01/2020
in PHP, Programming
PHP Loop (While, For, Foreach) Definition and Example

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

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:

  • 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
ShareTweetShare

Related Posts

Cara Mudah Memahami Bahasa Pemrograman Python
Python

Belajar Dasar-dasar Python untuk Pemula: Cara Mudah Memahami Bahasa Pemrograman Python

Python adalah bahasa pemrograman tingkat tinggi yang dapat digunakan untuk membuat berbagai macam aplikasi. Python diciptakan oleh Guido van Rossum...

06/03/2023
Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik
Belajar PHP

Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik

MySQL adalah database relational yang sangat populer di dunia programming hari ini. Dengan memiliki fitur yang bisa dikaitkan antar tabel,...

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

14. PHP Forms – Validation With Required Fields

Hi everyone, this time we will discuss about PHP Validation using the Required Field. Not many people know, it turns...

15/02/2020
PHP Form Handling
PHP

13. New Performing PHP Form Handling

Detriamelia.Com - Running form functions in PHP is not difficult. By using the $ _POST and $ _GET methods it...

13/02/2020
Next Post
PHP Function Definition and Example

10. PHP Function Definition and Example

PHP Array Example, Complete Editions

11. PHP Array Example, Complete Editions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended

Fungsi Peformatan Rupiah Pada Angka PHP

How to Make Rupiah Format Function for Number Using PHP

02/04/2019
PHP Array Example, Complete Editions

11. PHP Array Example, Complete Editions

17/01/2020
Adding Modern and Beautiful Link Effects with CSS3

Adding Modern and Beautiful Link Effects with CSS3

31/07/2019
GIS Tutorial with Leafletjs (Popup and Marker Modifications)

GIS Tutorial with Leafletjs (Popup and Marker Modifications)

29/07/2019

Instagram Feed

  • Sekolah sambil bermain...
  • Produk mahasiswa semester 5 Pendidikan Teknologi Informasi STKIP Rokania..
Kran dispenser otomatis (sensor)
#arduino
#PTI Ok
#STKIPRokania
#AyoKuliahDiRokania
  • Salah satu produk mahasiswa Pendidikan Teknologi Informasi STKIP Rokania...Pemanfaatan Internet Of Thing pada Lampu dan CCTV otomatis dengan Telegram Bot..

#StkipRokania
#PendidikanTeknologiInformasi
  • "Golden Time" yang tidak bisa diputar dan diulang tetapi tersimpan di inner child nya dan diingat selamanya..
  • Selamat atas M.Kom nya yah...semoga ada Reski untuk studi selanjutnya ya...
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
Detriamelia.Com

© 2022 Detri Amelia Chandra - IT Tips and Programming Tutorials.

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