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 the advantages and disadvantages of using JavaScript or HTML Meta-Tags instead of PHP for page redirection?
- What are some recommended resources or tutorials for learning how to create a contact form with browser data using PHP?
- How can PHP developers improve their understanding of file upload functionality by implementing simple scripts and gradually expanding their complexity?