Before looking while loop in PHP MCQs with Answers first of all we have to know what the while loop is, when we use while loop, and what is difference between while and do while loop.
What is while loop in PHP?
A while loop in PHP repeats a block of code as long as a specified condition is true. This checked condition before each iteration, and if it remains true, the loop continues. Once the condition becomes false, the loop terminates, and program execution proceeds to the next statement after the loop. This loop is important because the same code repeats many times.
Syntax of a While Loop in PHP
Following is a while loop in PHP.
while (condition) {
// Code to be executed
}
In while loop ‘condition‘ represents expression. If expression (Condition) is true, code in curly braces will execute. And code in curly braces will execute until the expression (Condition) is true.
While Loop in PHP with Example
Let me explain you, use of a while loop in PHP with an example.
<html>
<body>
<?php
$count = 1;
while ($count < 5) {
echo $count . "<br>";
$count++;
}
echo "Quizlancer";
?>
</body>
</html>
In above example, the while loop iterates as long as the variable ‘$count’ is less than 5. It increments the value of ‘$count’ in each iteration and echoes the countdown. Once ‘$count’ reaches 5, the loop terminates, and “Quizlancer” is displayed.
Result of above code like as below.
1
2
3
4
Quizlancer
While Loop in PHP MCQ Questions and Answers
What does a while loop in PHP do?
- Executes a block of code a fixed number of times
- Executes a block of code as long as a specified condition is true
- Executes a block of code only if a condition is true initially
- Executes a block of code continuously without any condition
Answer: 2. Executes a block of code as long as a specified condition is true
Which of the following statements best describes the condition in a while loop?
- It must always be a boolean value
- It must always be an integer value
- It must always be a string value
- It can be any expression that evaluates to a boolean value
Answer: 4. It can be any expression that evaluates to a boolean value
How do you terminate a while loop prematurely in PHP?
- Using the break statement
- Using the continue statement
- Using the end statement
- Using the terminate statement
Answer: 1. Using the break statement
What happens if the condition in a while loop is initially false?
- The loop executes once
- The loop executes twice
- The loop never executes
- The loop executes indefinitely
Answer: 3. The loop never executes
What is the main difference between a while loop and a do-while loop in PHP?
- The syntax
- The condition evaluation
- The number of iterations
- There is no difference
Answer: 2. The condition evaluation
In PHP, how do you initialize the loop control variable in a while loop?
- By using the initialize keyword
- By declaring it outside the loop
- By including it in the condition
- By assigning a value before the loop
Answer: 4. By assigning a value before the loop
What happens if you include an empty condition in a while loop in PHP?
- It will result in a syntax error
- The loop will execute indefinitely
- The loop will not execute at all
- PHP will throw a warning
Answer: 3. The loop will not execute at all
Consider the following PHP code snippet:
$num = 10;
while ($num >= 0) {
echo $num . " ";
$num -= 2;
}
What will be the output of the code?
- 10 8 6 4 2
- 10 8 6 4 2 0
- 10 9 8 7 6 5 4 3 2 1 0
- 10 9 8 7 6 5 4 3 2 1
Answer: 2. 10 8 6 4 2 0
Consider the following PHP code snippet:
$num = 1;
while ($num <= 5) {
echo $num . " ";
$num *= 2;
}
What will be the output of the code?
- 1 2 3 4 5
- 1 2 4
- 1 2 4 8
- 1 3 5
Answer: 2. 1 2 4
Follow Us on Telegram
Follow Quiz Lancer Telegram Channel to solve daily interview questions. We share important interview questions which are asked in the interview. Most of the students get good values from this channel and got selected as a developer.
Which loop is better (faster) while or do-while?
The difference between while and do while loops based on execution speed is that a do while loop runs faster than a while loop. The do-while is faster because it runs the first iteration without checking the loop condition. In contrast, the while loop checks the condition always.While loop takes time for check condition, in do while loop there is no need to check condition first. So as per this things do while loop is faster than while loop.