250+ TOP PHP Coding MCQs on Syntax and Answers

PHP Multiple Choice Questions on “Syntax – 2”.

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

  1. /*
  2. echo "Hello world";
  3. */
  4. ?>

a) Hello world
b) Nothing
c) Error
d)

/*
Hello world
*/

Answer: b
Clarification: /* */ is used for commenting multiple lines.

 
 

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

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

a) red red
b) red
c) error
d) nothing
Answer: c
Clarification: Use of undefined constant red.

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

  1. $color1 = red;
  2. $color2 = green;
  3. echo "$color1"."$color2";
  4. ?>

a) red green
b) red
c) green
d) error
Answer: d
Clarification: It has to be $color1 = “red”; and $color2 = “green”; therefore the error.

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

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

a) red
b) green
c) red green
d) error
Answer: b
Clarification: The variable contains the last value which has been assigned.

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

  1. $color1 = "red";
  2. $color2 = "green";
  3. echo "$color1" . "$color2";
  4. ?>

a) red
b) green
c) red green
d) redgreen
Answer: d
Clarification: The . operator is used to join to strings.

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

  1. $color1 = "red";
  2. $color2 = "green";
  3. echo "$color1" + "$color2";
  4. ?>

a) redgreen
b) red green
c) 0
d) error

Answer: c
Clarification: + operator does not join both the strings.

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

  1. $color1 = "red";
  2. $color2 = "red";
  3. echo "$color1" + "$color2";
  4. ?>

a) redgreen
b) red green
c) 0
d) 1
Answer: c
Clarification: + does not return 1 if the variables are equal.

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

  1. $color1 = "red";
  2. $color2 = "1";
  3. echo "$color1" + "$color2";
  4. ?>

a) red1
b) red 1
c) 0
d) 1
Answer: d
Clarification: + just returns the numeric value even though it is inside double quotes.

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

  1. $color1 = "1";
  2. $color2 = "1";
  3. echo "$color1" + "$color2";
  4. ?>

a) 11
b) 2
c) 0
d) 1
Answer: b
Clarification: + can be used to add to integer values which are enclosed by double-quotes.

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

  1. $color1 = "red";
  2. $color2 = "1";
  3. $color3 = "grey"
  4. echo "$color1" + "$color2" . "$color3";
  5. ?>

a) 1grey
b) grey
c) 0
d) red1grey
Answer: a
Clarification: + gives the value 1 and . is used to give join 1 and grey.

Leave a Comment

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

Scroll to Top