PHP Multiple Choice Questions on “Print”.
1. What will be the output of the following PHP code?
-
-
print "echo hello world";
-
?>
a) echo hello world
b) hello world
c) nothing
d) error
Answer: a
Clarification: The print statement will print whatever is present inside the double-quotes.
2. What will be the output of the following PHP code?
-
-
$one = 1;
-
print($one);
-
print $one;
-
?>
a) 01
b) 11
c) 10
d) Error
Answer: b
Clarification: Print can be used with or without parentheses.
3. What will be the output of the following PHP code?
-
-
$cars = array("Volvo", "BMW", "Toyota");
-
print $cars[2];
-
?>
a) Volvo
b) BMW
c) Toyota
d) Error
Answer: c
Clarification: Print statement can be used to output a specific array member.
4. What will be the output of the following PHP code?
-
-
$one = "one";
-
$two = "two";
-
print($one$two);
-
?>
a) onetwo
b) one
c) nothing
d) error
Answer: d
Clarification: The above syntax will produce an error, unlike the echo statement.
5. What will be the output of the following PHP code?
-
-
$one = "one";
-
$two = "two";
-
print($one,$two);
-
?>
a) onetwo
b) one, two
c) one
d) error
Answer: d
Clarification: The above syntax will produce an error, unlike the echo statement.
6. What will be the output of the following PHP code?
-
-
$one = "one";
-
$two = "two";
-
print("$one$two");
-
?>
a) onetwo
b) $one$two
c) one
d) error
Answer: a
Clarification: This is same as the echo statement.
7. What will be the output of the following PHP code?
-
-
$one = "one";
-
$two = "two";
-
print($one==$two);
-
?>
a) true
b) false
c) nothing
d) error
Answer: c
Clarification: Since we are equating two unequal strings we do not get any output.
8. What will be the output of the following PHP code?
-
-
$one = "one";
-
$two = "one";
-
print($one == $two);
-
?>
a) true
b) false
c) 1
d) error
Answer: c
Clarification: Since both the strings are equal the result 1 is printed on the screen.
9. What will be the output of the following PHP code?
-
-
print "Hello world!
"; -
print "I'm about to learn PHP!";
-
?>
a) Hello world!
I’m about to learn PHP!
b) Hello world! I’m about to learn PHP!
c)
Hello world! I'm about to learn PHP!
d) Error
Answer: c
Clarification: Most of the properties of echo and print are same. Strings can contain HTML markup.
10. What will be the output of the following PHP code?
-
-
print("this"."was"."a"."bad"."idea");
-
?>
a) thiswasabadidea
b) this was a bad idea
c) nothing
d) error
Answer: a
Clarification: You can use the dot operator like in echo but you can not use the comma operator to do the same.
