This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Syntax”.
1. What will be the output of the following PHP code?
<?php
"Hello World"
?>
a) Error
b) Hello World
c) Nothing
d) Missing semicolon error
View Answer
Explanation: 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?
<?php
print_r "Hello world"
?>
a) Error
b) Hello World
c) Nothing
d) Missing semicolon error
View Answer
Explanation: 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?
<?php
echo 'Hello World';
<html>
Hello world
</html>
?>
a) Hello world
b) Hello World Hello World
c)
Hello world Hello World
d) Syntax Error
View Answer
Explanation: 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?
<?php
Echo "Hello World1";
echo " Hello world2";
ECHO " Hello world3";
?>
a) Hello world1 Hello world2 Hello World3
b)
Hello world1 Hello world2 Hello World3
c) Error
d) Hello world1 Hello world3
View Answer
Explanation: 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?
<?php
$color = "red";
echo "$color";
echo "$COLOR";
echo "$Color";
?>
a) redredred
b) redred
c) red
d) Error
View Answer
Explanation: In PHP, all variables are case-sensitive.
6. What will be the output of the following PHP code?
<?php
# echo "Hello world";
echo "# Hello world";
?>
a) # Hello world
b) Hello world# Hello world
c) Hello world
d) Error
View Answer
Explanation: # is a single line comment.
7. What will be the output of the following PHP code?
<?php
echo "<i>Hello World</i>"
?>
a) Hello world
b) Hello world in italics
c) Nothing
d) Error
View Answer
Explanation: You can use tags like italics, bold etc. inside php script.
8. What will be the output of the following PHP code?
<?php
echo "echo "Hello World"";
?>
a) Hello world
b) echo “Hello world”
c) echo Hello world
d) Error
View Answer
Explanation: 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?
<?php
<?php
echo "Hello world";
?>
?>
a) HELLO WORLD
b) Hello world
c) Nothing
d) Error
View Answer
Explanation: You can not have php tags inside a php tag.
10. What will be the output of the following PHP code?
<?php
$color = red;
echo "\$color";
?>
a) red
b) $color
c) \red
d) Error
View Answer
Explanation: To print red remove the \.
More MCQs on PHP Syntax:
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all areas of PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Apply for Programming Internship
- Practice MCA MCQs
- Practice Programming MCQs
- Check PHP Books
- Check Information Technology Books