PHP Multiple Choice Questions on “Syntax – 2”.
1. What will be the output of the following PHP code?
-
-
/* -
echo "Hello world"; -
*/ -
?>
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?
-
-
$color = red;
-
echo "$color" . red ;
-
?>
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?
-
-
$color1 = red;
-
$color2 = green;
-
echo "$color1"."$color2";
-
?>
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?
-
-
$color = "red";
-
$color = "green";
-
echo "$color";
-
?>
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?
-
-
$color1 = "red";
-
$color2 = "green";
-
echo "$color1" . "$color2";
-
?>
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?
-
-
$color1 = "red";
-
$color2 = "green";
-
echo "$color1" + "$color2";
-
?>
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?
-
-
$color1 = "red";
-
$color2 = "red";
-
echo "$color1" + "$color2";
-
?>
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?
-
-
$color1 = "red";
-
$color2 = "1";
-
echo "$color1" + "$color2";
-
?>
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?
-
-
$color1 = "1";
-
$color2 = "1";
-
echo "$color1" + "$color2";
-
?>
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?
-
-
$color1 = "red";
-
$color2 = "1";
-
$color3 = "grey"
-
echo "$color1" + "$color2" . "$color3";
-
?>
a) 1grey
b) grey
c) 0
d) red1grey
Answer: a
Clarification: + gives the value 1 and . is used to give join 1 and grey.
