Bird
0
0

You want to read user input at least once and continue reading while the input is not empty. Which PHP do-while loop correctly implements this?

hard📝 Application Q15 of 15
PHP - Loops
You want to read user input at least once and continue reading while the input is not empty. Which PHP do-while loop correctly implements this?
do {
    $input = trim(fgets(STDIN));
    // process input
} ???;
Awhile ($input !== '');
Bwhile ($input);
Cwhile ($input == '');
Dwhile (!empty($input));
Step-by-Step Solution
Solution:
  1. Step 1: Understand the input condition

    We want to continue reading while input is not empty. The PHP function empty() returns true if input is empty.
  2. Step 2: Choose the correct condition

    Using while (!empty($input)); means loop continues while input has some content, stopping when empty.
  3. Final Answer:

    while (!empty($input)); -> Option D
  4. Quick Check:

    Loop continues while input is not empty [OK]
Quick Trick: Use while(!empty($input)) to loop until input is empty [OK]
Common Mistakes:
  • Using equality to empty string incorrectly
  • Using condition that stops loop too early
  • Confusing empty() with isset()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes