How can a while loop be terminated if a condition is met within the loop in PHP?
To terminate a while loop in PHP if a condition is met within the loop, you can use the `break` statement. This statement allows you to exit the loop prematurely if a specific condition is satisfied. By including an `if` statement within the loop that checks for the condition and then using `break` to exit the loop, you can effectively terminate the loop when needed.
$counter = 0;
while ($counter < 10) {
echo $counter . "<br>";
if ($counter == 5) {
break; // exit the loop if counter is equal to 5
}
$counter++;
}
Keywords
Related Questions
- What are some strategies to ensure that only specific rows of data are inserted into a database when multiple input fields and buttons are present on a form?
- What are some alternative methods for securely storing sensitive data, such as FTP and MySQL credentials, in PHP scripts?
- What are some best practices for handling Mime-Mails with attachments in PHP?