How can a specific key press be used to exit a loop in PHP?
To exit a loop in PHP based on a specific key press, you can utilize the `break` statement within an `if` condition that checks for the specific key press. This can be achieved by using the `fgets(STDIN)` function to read user input from the command line. If the specific key press is detected, the loop can be exited by using `break`.
<?php
// Loop example
while (true) {
$input = fgets(STDIN);
// Check for specific key press to exit loop
if ($input == 'q') {
break;
}
// Continue with loop execution
echo "Looping...\n";
}
echo "Loop exited.";
?>