PHP Multiple Choice Questions – Syntax

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

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

  1. <?php
  2. "Hello World"
  3. ?>

a) Error
b) Hello World
c) Nothing
d) Missing semicolon error
View Answer

Answer: c
Explanation: If you need to output something onto the screen you’ll need to use echo or print_r.

advertisement
advertisement

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

  1. <?php
  2. print_r "Hello world"
  3. ?>

a) Error
b) Hello World
c) Nothing
d) Missing semicolon error
View Answer

Answer: a
Explanation: The statement should be print_r(‘Hello World’) to print Hello world. Also if there is only one line then there is no requirement of a semicolon, but it is better to use it.

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

advertisement
  1. <?php
  2. echo 'Hello World';
  3. <html>
  4. Hello world
  5. </html>
  6. ?>

a) Hello world
b) Hello World Hello World
c)

Hello world
Hello World
advertisement

d) Syntax Error
View Answer

Answer: d
Explanation: Parse error: syntax error, unexpected ‘<‘ on line 2. You can not use the html tag inside php tags.

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

  1. <?php
  2. Echo "Hello World1";
  3. echo " Hello world2";
  4. ECHO " Hello world3";
  5. ?>

a) Hello world1 Hello world2 Hello World3
b)

Hello world1 
Hello world2 
Hello World3

c) Error
d) Hello world1 Hello world3
View Answer

Answer: a
Explanation: In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.) are case-insensitive.

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

  1. <?php
  2. $color = "red";
  3. echo "$color";
  4. echo "$COLOR";
  5. echo "$Color";
  6. ?>

a) redredred
b) redred
c) red
d) Error
View Answer

Answer: c
Explanation: In PHP, all variables are case-sensitive.

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

  1. <?php
  2.  # echo "Hello world";
  3.  echo "# Hello world"; 
  4. ?>

a) # Hello world
b) Hello world# Hello world
c) Hello world
d) Error
View Answer

Answer: a
Explanation: # is a single line comment.

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

  1. <?php
  2. echo "<i>Hello World</i>"
  3. ?>

a) Hello world
b) Hello world in italics
c) Nothing
d) Error
View Answer

Answer: b
Explanation: You can use tags like italics, bold etc. inside php script.

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

  1. <?php
  2. echo "echo "Hello World"";
  3. ?>

a) Hello world
b) echo “Hello world”
c) echo Hello world
d) Error
View Answer

Answer: d
Explanation: It would have printed echo “Hello world” if we have put backslash doublequotes just before and just after Hello World string.

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

  1. <?php
  2. <?php
  3. echo "Hello world";
  4. ?>
  5. ?>

a) HELLO WORLD
b) Hello world
c) Nothing
d) Error
View Answer

Answer: d
Explanation: You can not have php tags inside a php tag.

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

  1. <?php
  2. $color = red;
  3. echo "\$color";
  4. ?>

a) red
b) $color
c) \red
d) Error
View Answer

Answer: b
Explanation: To print red remove the \.

More MCQs on PHP Syntax:

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.