You need to execute a block of code at least once in your PHP script, then repeat it as long as a certain condition is true. Which type of loop would you use and why?

  • do-while loop
  • for loop
  • while loop
  • foreach loop
If you need to execute a block of code at least once and then repeat it as long as a certain condition is true, you would use a do-while loop in PHP. The do-while loop guarantees that the code block is executed at least once, regardless of the condition. After the first execution, the condition is checked. If the condition evaluates to true, the loop will repeat. If the condition evaluates to false, the loop will terminate. The do-while loop is suitable for scenarios where you want to ensure the execution of a specific code block at least once before checking the condition for further iterations. Learn more: https://www.php.net/manual/en/control-structures.do.while.php
Add your answer
Loading...

Leave a comment

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