250+ TOP PHP Coding MCQs on Syntax and Answers

PHP Multiple Choice Questions on “Syntax – 1”.

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

a) Error
b) Hello World
c) Nothing
d) Missing semicolon error
Answer: c
Clarification: If you need to output something onto the screen you’ll need to use echo or print_r.

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

  1. print_r "Hello world"
  2. ?>

a) Error
b) Hello World
c) Nothing
d) Missing semicolon error
Answer: a
Clarification: 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?

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

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

Hello world
Hello World

d) Syntax Error
Answer: d
Clarification: 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. Echo "Hello World1";
  2. echo " Hello world2";
  3. ECHO " Hello world3";
  4. ?>

a) Hello world1 Hello world2 Hello World3
b)

Hello world1 
Hello world2 
Hello World3

c) Error
d) Hello world1 Hello world3
Answer: a
Clarification: 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. $color = "red";
  2. echo "$color";
  3. echo "$COLOR";
  4. echo "$Color";
  5. ?>

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

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

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

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

a) # Hello world
b) Hello world# Hello world
c) Hello world
d) Error
Answer: a
Clarification: # is a single line comment.

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

  1. echo "Hello World"
  2. ?>

a) Hello world
b) Hello world in italics
c) Nothing
d) Error
Answer: b
Clarification: You can use tags like italics, bold etc. inside php script.

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

  1. echo "echo "Hello World"";
  2. ?>

a) Hello world
b) echo “Hello world”
c) echo Hello world
d) Error
Answer: d
Clarification: 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. echo "Hello world";
  2. ?>
  3. ?>

a) HELLO WORLD
b) Hello world
c) Nothing
d) Error
Answer: d
Clarification: You can not have php tags inside a php tag.

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

  1. $color = red;
  2. echo "$color";
  3. ?>

a) red
b) $color
c) red
d) Error
Answer: b
Clarification: To print red remove the .

Leave a Comment

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

Scroll to Top