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

Learn Python Basics for Beginners: Easy Ways to Understand the Python Programming Language
Python

Learn Python Basics for Beginners: Easy Ways to Understand the Python Programming Language

Python is a high-level programming language that can be used to create a wide range of applications. Python was created...

09/08/2023
Database Table Relations Using Join Operations and the Classical Model
Belajar PHP

Database Table Relations Using Join Operations and the Classical Model

MySQL is a highly popular relational database in today's programming world. With features that allow for table-to-table relationships, MySQL databases...

09/08/2023
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

install-apache-mysql-and-php-in-ubuntu-18-04-lts-part-2

3. How to Install Apache, MySQL and PHP in Ubuntu 18.04 LTS Part 2

11/11/2019
What You Must Know About PHP

1. What You Must Know About PHP

10/11/2019
Fungsi Peformatan Rupiah Pada Angka PHP

How to Make Rupiah Format Function for Number Using PHP

02/04/2019
Introduction to Python Programming: A Beginner's Guide

Introduction to Python Programming: A Beginner’s Guide

16/08/2023

Instagram Feed

    The Instagram Access Token is expired, Go to the Customizer > JNews : Social, Like & View > Instagram Feed Setting, to refresh it.
  • 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
SV388 AGEN TERPERCAYA LINK SV388 RESMI AGEN S128 RESMI MAMI188 AGEN SABUNG AYAM https://web.man1tapin.sch.id/ SLOT GACOR SLOT QRIS https://man1tapin.sch.id/