PHP Coding Questions and Answers – Operators – 2

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Operators – 2”.

1. What will be the output of the following PHP code?

  1. <?php
  2. $i = 0;
  3. while ($i = 10)
  4. {   
  5.     print "hi";
  6. }
  7. print "hello";
  8. ?>

a) hello
b) infinite loop
c) hihello
d) error
View Answer

Answer: b
Explanation: While condition always gives 1.
advertisement
advertisement

2. What will be the output of the following PHP code?

  1. <?php
  2. $i = "";
  3. while ($i = 10)
  4. {   
  5.     print "hi";
  6. }
  7. print "hello";
  8. ?>

a) hello
b) infinite loop
c) hihello
d) error
View Answer

Answer: b
Explanation: While condition always gives 1.
Note: Join free Sanfoundry classes at Telegram or Youtube

3. What will be the output of the following PHP code?

advertisement
  1. <?php
  2. $i = 5;
  3. while (--$i > 0)
  4. {   
  5.     $i++;
  6.     print $i;
  7.     print "hello";
  8. }
  9. ?>

a) 4hello4hello4hello4hello4hello…..infinite
b) 5hello5hello5hello5hello5hello…..infinite
c) no output
d) error
View Answer

Answer: a
Explanation: i is decremented in the first while execution and then continuously incremented back.
advertisement

4. What will be the output of the following PHP code?

  1. <?php
  2. $i = 5;
  3. while (--$i > 0 && ++$i)
  4. {   
  5.     print $i;
  6. }
  7. ?>

a) 5
b) 555555555…infinitely
c) 54321
d) error
View Answer

Answer: b
Explanation: As it is && operator it is being incremented and decremented continuously.

5. What will be the output of the following PHP code?

  1. <?php
  2. $i = 5;
  3. while (--$i > 0 || ++$i)
  4. {   
  5.     print $i;
  6. }
  7. ?>

a) 54321111111….infinitely
b) 555555555…infinitely
c) 54321
d) 5
View Answer

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

6. What will be the output of the following PHP code?

  1. <?php
  2. $i = 0;
  3. while(++$i || --$i)
  4. {   
  5.     print $i;
  6. }
  7. ?>

a) 1234567891011121314….infinitely
b) 01234567891011121314…infinitely
c) 1
d) 0
View Answer

Answer: a
Explanation: As it is || operator the second expression is not evaluated and i is always incremented, in the first case to 1.

7. What will be the output of the following PHP code?

  1. <?php
  2. $i = 0;
  3. while (++$i && --$i)
  4. {   
  5.     print $i;
  6. }
  7. ?>

a) 1234567891011121314….infinitely
b) 01234567891011121314…infinitely
c) no output
d) error
View Answer

Answer: c
Explanation: The first condition itself fails thus the loop exits.

8. What will be the output of the following PHP code?

  1. <?php
  2. $i = 0;
  3. while ((--$i > ++$i) - 1)
  4. {   
  5.     print $i;
  6. }
  7. ?>

a) 00000000000000000000….infinitely
b) -1-1-1-1-1-1-1-1-1-1…infinitely
c) no output
d) error
View Answer

Answer: a
Explanation: (–$i > ++$i) evaluates to 0 but -1 makes it enters the loop and prints i which is 0.

9. What will be the output of the following PHP code?

  1. <?php
  2. $i = 2;
  3. while (++$i)
  4. {   
  5.     while ($i --> 0)
  6.         print $i;
  7. }
  8. ?>

a) 210
b) 10
c) no output
d) infinite loop
View Answer

Answer: a
Explanation: The loop ends when i becomes 0.

10. What will be the output of the following PHP code?

  1. <?php
  2. $i = 2;
  3. while (++$i)
  4. {   
  5.     while (--$i > 0)
  6.         print $i;
  7. }
  8. ?>

a) 210
b) 10
c) no output
d) infinite loop
View Answer

Answer: d
Explanation: The loop never ends as i is always incremented and then decremented.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.