Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.

Get Started Now!

What is PHP Iterative Statements and how many types of Iterative statements:

PHP Iterative Statements, also known as loops, are used to execute a block of code repeatedly as long as a specified condition is true. PHP supports four types of iterative statements:

  1. while Loop
  2. do-while Loop
  3. for Loop
  4. foreach loop

while Loop:

The while loop is used when you want to execute a block of code as long as a specified condition is true. The show is given follow chat

Syntax:

while (condition) 
{
  // code to be executed
}

Example:

<?php
$x = 1;
while($x <= 10) {
  echo "The number is: $x <br>";
  $x++;
}
?>

do-while Loop:

The do-while loop is similar to the while loop, but the block of code is executed at least once, regardless of whether the condition is true or false.

Syntax:

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

Example:

<?php
$x =10;
do {
  echo "The number is: $x <br>";
  $x++;
} while ($x <= 9);
?>

for Loop:

The for loop is used when you know exactly how many times you want to execute a block of code. It has a counter variable that is incremented or decremented with each iteration.

Syntax:

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

Example:

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

For-each loop:

The foreach loop is used to loop through arrays and objects. It iterates over each element in the array or each property in the object.

Syntax:

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

Example:

<?php
echo "Welcome to the world of foreach loops <br>";
$arr = array("Bananas", "Apples", "Harpic", "Bread", "Butter");
foreach ($arr as  $value) {
    echo "$value <br>";
}
?>

Related Posts

How to Install Node.js and npm (Step-by-Step Guide)

How to Install Node.js and npm (Step-by-Step Guide) Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine, and npm (Node Package Manager) is the package Read More

Read More

Creating a WordPress Post with Google Image Search Results in PHP

In this blog post, we will explore a PHP script that utilizes the Google Custom Search API to search for images based on user-defined keywords. We’ll also Read More

Read More

How to Automatically Generate Image Search Queries Based on Keywords Using Google API

To automatically generate image search queries using Google API, you can use the Custom Search JSON API. Here’s a step-by-step guide: Step 1: Create a Google Cloud Read More

Read More

How to Build an Image Search Engine with Google Custom Search API and PHP

Building an image search engine using the Google Custom Search API and PHP involves several steps. Here is a detailed guide: Prerequisites Step 1: Create a Custom Read More

Read More

What is Composer, and why is it used in PHP development?

Composer is a dependency management tool specifically designed for PHP. It streamlines the process of managing libraries and packages required for PHP projects, making development more efficient Read More

Read More

Understanding PHP Data Types: A Comprehensive Guide with Examples

In PHP, data types refer to the various kinds of data that can be stored and manipulated within the language. PHP is a loosely typed language, meaning Read More

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x