PHP Coding Questions and Answers – Syntax – 2

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

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

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

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

/*
Hello world
*/
View Answer
Answer: b
Explanation: /* */ is used for commenting multiple lines.
 
 

advertisement
advertisement

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

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

a) red red
b) red
c) error
d) nothing
View Answer

Answer: c
Explanation: Use of undefined constant red.

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

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

a) red green
b) red
c) green
d) error
View Answer

Answer: d
Explanation: It has to be $color1 = “red”; and $color2 = “green”; therefore the error.
advertisement

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

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

a) red
b) green
c) red green
d) error
View Answer

Answer: b
Explanation: The variable contains the last value which has been assigned.

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

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

a) red
b) green
c) red green
d) redgreen
View Answer

Answer: d
Explanation: The . operator is used to join to strings.

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

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

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

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

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

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

a) redgreen
b) red green
c) 0
d) 1
View Answer

Answer: c
Explanation: + does not return 1 if the variables are equal.

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

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

a) red1
b) red 1
c) 0
d) 1
View Answer

Answer: d
Explanation: + just returns the numeric value even though it is inside double quotes.

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

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

a) 11
b) 2
c) 0
d) 1
View Answer

Answer: b
Explanation: + 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. <?php
  2. $color1 = "red";
  3. $color2 = "1";
  4. $color3 = "grey"
  5. echo "$color1" + "$color2" . "$color3";
  6. ?>

a) 1grey
b) grey
c) 0
d) red1grey
View Answer

Answer: a
Explanation: + gives the value 1 and . is used to give join 1 and grey.

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.

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.