Introduction:
Explore the power of PHP loops with our in-depth tutorial. Learn about while loops, do-while loops, and the effective use of break and continue statements. Elevate your PHP programming skills with hands-on examples and practical insights.
In PHP, a do-while loop is used to repeatedly execute a block of code as long as a specified condition is true. The key difference between a do-while loop and a while loop is that the do-while loop guarantees that the block of code is executed at least once, even if the condition is initially false.
Here’s the basic syntax of a do-while loop in PHP:
do { // Code to be executed } while (condition);
The flow of execution is as follows:
The code block inside the do is executed.
The condition is checked after the code block execution.
If the condition is true, the loop continues to iterate, and the code block is executed again.
If the condition is false, the loop exits.
Here’s a simple example to illustrate the do-while loop:
<?php $counter = 1; do { echo "This is iteration number: $counter <br>"; $counter++; } while ($counter <= 5); ?>
In this example, the loop will print the message and increment the $counter variable as long as it is less than or equal to 5.
It’s important to note that the condition is evaluated after the code block is executed, so the code block will always execute at least once, regardless of the initial state of the condition.
A complete example in html with explanation
Here’s a complete example with HTML and PHP, along with explanations for each part:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP do-while Loop Example</title> </head> <body> <h1>PHP do-while Loop Example</h1> <?php // Initialize a counter variable $counter = 1; // The do-while loop do { // Code to be executed inside the loop echo "This is iteration number: $counter <br>"; // Increment the counter $counter++; } while ($counter <= 5); ?> </body> </html>
Explanation:
<!DOCTYPE html>: Declares the HTML version to be used (HTML5).
<html lang=”en”>: The root HTML element with the language attribute set to English.
<head>: Contains meta-information about the HTML document, such as character set and viewport settings.
<title>: Sets the title of the HTML document, which appears in the browser tab.
<body>: Contains the content of the HTML document that will be displayed in the browser.
The PHP code block inside the <body> element:
<?php … ?>: This is the PHP opening and closing tag.
$counter = 1;: Initializes a counter variable to 1.
do { … } while ($counter <= 5);: The do-while loop. The code inside the do block will be executed at least once, and the loop will continue as long as the condition $counter <= 5 is true.
echo “This is iteration number: $counter <br>”;: Outputs a message with the current value of the counter variable.
$counter++;: Increments the counter variable by 1 in each iteration.
The HTML code outside the PHP block:
<h1>PHP do-while Loop Example</h1>: Adds a heading to the HTML page.
The PHP block outputs messages inside the <body> element.
When you run this PHP script, you’ll see a web page with a heading and messages indicating the iterations of the do-while loop. The loop ensures that the message is displayed at least once
Here is the basic syntax of the break statement:
<?php while (condition) { // Code to be executed if (/* some condition */) { break; } // More code } ?>
Or within a do-while loop:
<?php do { // Code to be executed if (/* some condition */) { break; } // More code } while (condition); ?>
And within a for loop:
<?php for ($i = 0; $i < $limit; $i++) { // Code to be executed if (/* some condition */) { break; } // More code } ?>
Explanation:
The break statement can be used in while, do-while, and for loops.
Here’s a simple example using a while loop:
<?php $counter = 1; while ($counter <= 10) { echo "Current counter value: $counter <br>"; if ($counter === 5) { echo "Breaking out of the loop at counter = 5 <br>"; break; } $counter++; } ?>
In this example, the loop will output messages for the first five iterations, and then it will break out of the loop when the counter reaches 5. The break statement interrupts the loop’s normal flow and exits it immediately.
complete example in html with explanation
Here’s a complete example with HTML and PHP that demonstrates the use of the break statement within a while loop:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP break Statement Example</title> </head> <body> <h1>PHP break Statement Example</h1> <?php // Initialize a counter variable $counter = 1; // The while loop with a break statement while ($counter <= 10) { // Code to be executed inside the loop echo "Current counter value: $counter <br>"; // Check if the counter is 5 if ($counter === 5) { echo "Breaking out of the loop at counter = 5 <br>"; // Break out of the loop when counter is 5 break; } // Increment the counter $counter++; } ?> </body> </html>
Explanation:
<!DOCTYPE html>: Declares the HTML version to be used (HTML5).
<html lang=”en”>: The root HTML element with the language attribute set to English.
<head>: Contains meta-information about the HTML document, such as character set and viewport settings.
<title>: Sets the title of the HTML document, which appears in the browser tab.
<body>: Contains the content of the HTML document that will be displayed in the browser.
The PHP code block inside the <body> element:
<?php … ?>: This is the PHP opening and closing tag.
$counter = 1;: Initializes a counter variable to 1.
while ($counter <= 10) { … }: The while loop. The code inside the loop will be executed as long as the condition $counter <= 10 is true.
echo “Current counter value: $counter <br>”;: Outputs a message with the current value of the counter variable.
if ($counter === 5) { … }: Checks if the counter is equal to 5.
echo “Breaking out of the loop at counter = 5 <br>”;: Outputs a message indicating that the loop is breaking when the counter is 5.
break;: Breaks out of the loop prematurely
Here’s an example that uses the break statement within a do-while loop in PHP, embedded in an HTML page:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP break Statement with do-while Loop Example</title> </head> <body> <h1>PHP break Statement with do-while Loop Example</h1> <?php // Initialize a counter variable $counter = 1; // The do-while loop with a break statement do { // Code to be executed inside the loop echo "Current counter value: $counter <br>"; // Check if the counter is 3 if ($counter === 3) { echo "Breaking out of the loop at counter = 3 <br>"; // Break out of the loop when counter is 3 break; } // Increment the counter $counter++; } while ($counter <= 5); ?> </body> </html>
Explanation:
<!DOCTYPE html>: Declares the HTML version to be used (HTML5).
<html lang=”en”>: The root HTML element with the language attribute set to English.
<head>: Contains meta-information about the HTML document, such as character set and viewport settings.
<title>: Sets the title of the HTML document, which appears in the browser tab.
<body>: Contains the content of the HTML document that will be displayed in the browser.
The PHP code block inside the <body> element:
<?php … ?>: This is the PHP opening and closing tag.
$counter = 1;: Initializes a counter variable to 1.
do { … } while ($counter <= 5);: The do-while loop. The code inside the loop will be executed at least once, and then the loop will continue as long as the condition $counter <= 5 is true.
`echo “Current counter
Here’s an example demonstrating the continue statement within a do-while loop:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP continue Statement with do-while Loop Example</title> </head> <body> <h1>PHP continue Statement with do-while Loop Example</h1> <?php // Initialize a counter variable $counter = 1; // The do-while loop with a continue statement do { // Check if the counter is even if ($counter % 2 === 0) { // Skip the rest of the loop for even numbers $counter++; continue; } // Code to be executed for odd numbers echo "Current counter value (odd): $counter <br>"; // Increment the counter $counter++; } while ($counter <= 5); ?> </body> </html>
Explanation:
<!DOCTYPE html>: Declares the HTML version to be used (HTML5).
<html lang=”en”>: The root HTML element with the language attribute set to English.
<head>: Contains meta-information about the HTML document, such as character set and viewport settings.
<title>: Sets the title of the HTML document, which appears in the browser tab.
<body>: Contains the content of the HTML document that will be displayed in the browser.
The PHP code block inside the <body> element:
<?php … ?>: This is the PHP opening and closing tag.
$counter = 1;: Initializes a counter variable to 1.
do { … } while ($counter <= 5);: The do-while loop. The code inside the loop will be executed at least once, and then the loop will continue as long as the condition $counter <= 5 is true.
if ($counter % 2 === 0) { … }: Checks if the counter is even using the modulo operator (%).
continue;: Skips the rest of the loop for even numbers and jumps to the next iteration.
echo “Current counter value (odd): $counter <br>”;: Outputs a message for odd numbers.
$counter++;: Increments the counter variable by 1 in each iteration.
The HTML code outside the PHP block:
<h1>PHP continue Statement with do-while Loop Example</h1>: Adds a heading to the HTML page.
The PHP block outputs messages inside the <body> element.
When you run this PHP script, you’ll see a web page with a heading and messages indicating the iterations of the do-while loop. The loop skips the rest of the code for even numbers and continues with the next iteration.
For simplicity, we’ll create a web application that allows the to input a number and then uses a do-while loop to display a message for each iteration, breaking out of the loop when a specific condition is met, and using the continue statement to skip iterations.
index.php:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP Loop Application</title> </head> <body> <h1>PHP Loop Application</h1> <form method="post" action="process.php"> <label for="number">Enter a number:</label> <input type="number" name="number" id="number" required> <button type="submit">Submit</button> </form> </body> </html> process.php: php <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { // Retrieve the number from the form $Number = $_POST["number"]; // Validate if the input is a positive integer if (!is_numeric($Number) || $Number < 1 || floor($Number) != $Number) { echo "Please enter a valid positive integer."; } else { // Initialize a counter variable $counter = 1; // The do-while loop with break and continue statements do { // Check if the counter is divisible by 3 if ($counter % 3 === 0) { // Skip this iteration for numbers divisible by 3 $counter++; continue; } // Code to be executed inside the loop echo "Iteration $counter: "; // Check if the counter is 5 if ($counter === 5) { echo "Breaking out of the loop at counter = 5 <br>"; // Break out of the loop when counter is 5 break; } // Display a message for the current iteration echo "Displaying message for counter = $counter <br>"; // Increment the counter $counter++; } while ($counter <= $Number); } } ?>
Explanation:
You can save these files in the same directory on your web server and access the application through your browser. The index.php file presents the form, and process.php processes the form data and performs the loop logic based on input.
Here’s a set of 15 quiz questions related to PHP loops (while, do-while) and break/continue statements.Each question is followed by multiple-choice options.
Choose the correct option for each question.
A) To execute a block of code repeatedly while a condition is true.
B) To execute a block of code at least once, then repeat as long as a condition is true.
C) To execute a block of code a specific number of times.
A) for loop
B) while loop
C) do-while loop
A) Before executing the code block
B) After executing the code block
C) While executing the code block
A) Skips the rest of the loop and moves to the next iteration.
B) Exits the loop prematurely, regardless of the loop condition.
C) Continues to the next iteration of the loop.
A) continue exits the loop prematurely; break skips the current iteration.
B) continue skips the rest of the loop and moves to the next iteration; break exits the loop.
C) continue and break are interchangeable.
A) Before executing the code block
B) After executing the code block
C) While executing the code block
A) It always executes the code block at least once.
B) It never executes the code block.
C) It executes the code block only if the condition is true initially.
A) To repeat a block of code a specific number of times.
B) To execute a block of code repeatedly while a condition is true.
C) To execute a block of code at least once, then repeat as long as a condition is true.
A) for loop
B) while loop
C) do-while loop
A) There is no difference.
B) The condition is placed after the do keyword.
C) The condition is placed before the do keyword.
A) The loop runs indefinitely.
B) The loop will not execute.
C) The loop continues to the next iteration.
A) Exits the loop.
B) Skips the rest of the loop and moves to the next iteration.
C) Repeats the current iteration.
A) break and continue can only be used in for loops.
B) break and continue can be used in any type of loop (for, while, do-while).
C) break and continue are not valid in PHP.
A) It is skipped, and the loop continues with the next iteration.
B) It is executed, and the loop continues with the next iteration.
C) The loop terminates.
A) for loop
B) while loop
C) do-while loop