What is the purpose of the "break" statement in PHP?
The "break" statement in PHP is used to exit a loop prematurely. It is commonly used in switch statements to exit the switch block. By using the "break" statement, you can stop the execution of the loop or switch statement and continue with the rest of the code. Example:
$colors = array("red", "green", "blue");
foreach ($colors as $color) {
if ($color == "green") {
break; // exit the loop if the color is green
}
echo $color . "<br>";
}
Keywords
Related Questions
- What best practices should be followed when using simplexml_load_file() function in PHP to parse XML data?
- What are some common string manipulation functions in PHP that can be used to extract a portion of a string?
- How can PHP be used to compare an uploaded image with existing images in a directory before moving it?