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:

  • while Loop
  • do-while Loop
  • for Loop
  • 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.

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

example with programming:

<?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>";
}
?>

Leave a Reply

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