You need to execute a block of code in your PHP script for an unknown number of times, but at least once. How would you do this using a while loop and why might you choose it over a for loop?

  • Use a while loop with a condition that evaluates to true
  • Use a while loop with a condition that evaluates to false
  • Use a for loop with a fixed number of iterations
  • Use a do...while loop with a condition that evaluates to false
To execute a block of code for an unknown number of times, but at least once, in PHP, you can use a while loop with a condition that evaluates to true. This ensures that the code block is executed at least once, and if the condition remains true, the block of code will continue to execute repeatedly until the condition becomes false. You might choose a while loop over a for loop in this scenario when you don't have a fixed number of iterations or when the termination condition depends on dynamic factors that cannot be determined in advance. Learn more: https://www.php.net/manual/en/control-structures.while.php
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *