MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

PHP 7 Fundamental Tutorial for Beginners – PHP Loop

What is Loops?

Diagram explaining what a loop is in PHP: code repeated while a condition is met

Loops are used to execute the same block of code again and again, as long as a certain condition is met. The basic idea behind a loop is to automate the repetitive tasks within a program to save the time and effort. PHP supports four different types of loops.

Diagram showing the parts of a PHP loop

Loops are used to execute the same block of code again and again, as long as a certain condition is true.

Different Types of Loops in PHP

Overview of PHP loop types: while, do-while, for and foreach
  • while — loops through a block of code as long as the condition specified evaluates to true.
  • do…while — the block of code executed once and then condition is evaluated. If the condition is true the statement is repeated as long as the specified condition is true.
  • for — loops through a block of code until the counter reaches a specified number.
  • foreach — loops through a block of code for each element in an array.

PHP while Loop

The while statement will loops through a block of code as long as the condition specified in the while statement evaluate to true.

Diagram of PHP while loop parts and flow

Syntax

while (condition is true) {
  code to be executed;
}

This Example of Increment

PHP while loop example incrementing a counter variable and its output

Example:-

This Example of Decrement

PHP while loop example decrementing a counter variable and its output

PHP do…while Loop

Flowchart of a PHP do-while loop running code once before checking the condition

The do-while loop is a variant of while loop, which evaluates the condition at the end of each loop iteration. With a do-while loop the block of code executed once, and then the condition is evaluated, if the condition is true, the statement is repeated as long as the specified condition evaluated to is true.

PHP do-while loop syntax diagram

Syntax

do {
  code to be executed;
} while (condition is true);


The following example define a loop that starts with $a=1. It will then increase $a with 1, and print the output. Then the condition is evaluated, and the loop will continue to run as long as $a is less than, or equal to 10.

Examples

This Example of Increment

PHP do-while example incrementing $a from 1 up to 10 with output

This Example of Decrement

PHP do-while example decrementing a counter with output

PHP for Loop

The for loop repeats a block of code as long as a certain condition is met. It is typically used to execute a block of code for certain number of times.

Syntax

for(initialization; condition; increment){
    // Code to be executed
}

The parameters of for loop have following meanings:

PHP for loop syntax showing initialization, condition and increment parts
  • initialization — it is used to initialize the counter variables, and evaluated once unconditionally before the first execution of the body of the loop.
  • condition — in the beginning of each iteration, condition is evaluated. If it evaluates to true, the loop continues and the nested statements are executed. If it evaluates to false, the execution of the loop ends.
  • increment — it updates the loop counter with a new value. It is evaluate at the end of each iteration.

The example below defines a loop that starts with $a=1. The loop will continue until $a is less than, or equal to 10. The variable $a will increase by 1 each time the loop runs:

PHP for loop example printing $a from 1 to 10

Example:-

PHP for loop example decrementing a counter with output

PHP foreach Loop

The foreach loop is used to iterate over arrays.

PHP foreach loop example iterating over an array

Syntax:-

foreach ($array as$value) {
  code to be executed;
}

PHP foreach loop syntax example printing array values

The following example demonstrates a loop that will print the values of the given array:

Example:-

Output of PHP foreach loop printing each value of the array

There is one more syntax of foreach loop, which is extension of the first.

Syntax:-

foreach($array as $key => $value){
    // Code to be executed
}

Example:-

PHP foreach loop example using key => value syntax with its output

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Encryption using RSA public and Private Key using PHP

PHP's OpenSSL extension can create an RSA key pair, encrypt data with the public key and decrypt it with the private key. This page explains the gist…

Read More

Encryption using Public key and Private key in PHP using mySql database

This tutorial shows how to generate an RSA key pair with PHP's OpenSSL extension, encrypt form input with the public key, store the ciphertext in MySQL, and…

Read More

How to see number of emails sent using smtp in laravel

Laravel does not count or log SMTP emails by default, so a grep will only return a real number once your application writes one log line for…

Read More

What are the Authentication type in phpmyadmin which for config.inc.php

phpMyAdmin supports four authentication types, set per server with $cfg['Servers'][$i]['auth_type'] in config.inc.php: cookie (the default), config, http and signon. This page explains how each one works and…

Read More

What is opcache and Install / Configure in Linux

OPcache is PHP's built-in bytecode cache. It keeps compiled scripts in shared memory so PHP doesn't have to parse and compile the same files on every request….

Read More

PHP Code for Godaddy VPS Server SMTP Email Relay

This page shows how to send email from a GoDaddy VPS through GoDaddy's SMTP relay on port 25 with plain PHP sockets and no extra libraries. Below…

Read More