Operators And Expressions In Php - Study Mode

[#121] What will be the output of the following PHP code ? <?php $i = 0
while ( $i = 10 ) { print "hi"
} print "hello"
?>
Correct Answer

(B) infinite loop

Explanation

Solution: While condition always gives 1.

[#122] What will be the output of the following PHP code ? <?php $i = ""
while ( $i = 10 ) { print "hi"
} print "hello"
?>
Correct Answer

(B) infinite loop

Explanation

Solution: While condition always gives 1.

[#123] What will be the output of the following PHP code ? <?php $i = 5
while (-- $i > 0 ) { $i ++
print $i
print "hello"
} ?>
Correct Answer

(A) 4hello4hello4hello4hello4hello…..infinite

Explanation

Solution: i is decremented in the first while execution and then continuously incremented back.

[#124] What will be the output of the following PHP code ? <?php $i = 5
while (-- $i > 0 && ++ $i ) { print $i
}< ?>
Correct Answer

(B) 555555555…infinitely

Explanation

Solution: As it is && operator it is being incremented and decremented continuously.

[#125] What will be the output of the following PHP code ? <?php $i = 5
while (-- $i > 0 || ++ $i ) { print $i
} ?>
Correct Answer

(A) 54321111111….infinitely

Explanation

Solution: As it is || operator the second expression is not evaluated till i becomes 1 then it goes into a loop.