Here are 1000 MCQs on PHP (Chapterwise).
1. What is PHP?
a) PHP is an open-source programming language
b) PHP is used to develop dynamic and interactive websites
c) PHP is a server-side scripting language
d) All of the mentioned
View Answer
Explanation: PHP is an open-source server-side scripting language that is used to build dynamic and interactive web pages or web applications.
2. Who is the father of PHP?
a) Drek Kolkevi
b) Rasmus Lerdorf
c) Willam Makepiece
d) List Barely
View Answer
Explanation: PHP was originally created by Rasmus Lerdorf in 1994.
3. What does PHP stand for?
a) PHP stands for Preprocessor Home Page
b) PHP stands for Pretext Hypertext Processor
c) PHP stands for Hypertext Preprocessor
d) PHP stands for Personal Hyper Processor
View Answer
Explanation: PHP previously stood for Personal Home Page now stands for “Hypertext Preprocessor”.
4. Which of the following is the correct syntax to write a PHP code?
a) <?php ?>
b) < php >
c) < ? php ?>
d) <? ?>
View Answer
Explanation: Every section of PHP code starts and ends by turning on and off PHP tags to let the server know that it needs to execute the PHP in between them.
5. Which of the following is the correct way to add a comment in PHP code?
a) #
b) //
c) /* */
d) All of the mentioned
View Answer
Explanation: In PHP, /* */ can also be used to comment just a single line although it is used for paragraphs. // and # are used only for single-line comments.
6. Which of the following is the default file extension of PHP files?
a) .php
b) .ph
c) .xml
d) .html
View Answer
Explanation: To run a PHP file on the server, it should be saved as AnyName.php
7. How to define a function in PHP?
a) functionName(parameters) {function body}
b) function {function body}
c) function functionName(parameters) {function body}
d) data type functionName(parameters) {function body}
View Answer
Explanation: PHP allows us to create our own user-defined functions. Any name ending with an open and closed parenthesis is a function. The keyword function is always used to begin a function.
8. What will be the output of the following PHP code?
<?php $x = 10; $y = 20; if ($x > $y && 1||1) print "1000 PHP MCQ" ; else print "Welcome to Sanfoundry"; ?>
a) no output
b) Welcome to Sanfoundry
c) 1000 PHP MCQ
d) error
View Answer
Explanation: Expression evaluates to true.
Output:
1000 PHP MCQ
9. Which is the right way of declaring a variable in PHP?
a) $3hello
b) $_hello
c) $this
d) $5_Hello
View Answer
Explanation: A variable in PHP can not start with a number, also $this is mainly used to refer properties of a class so we can’t use $this as a user defined variable name.
10. What will be the output of the following PHP program?
<?php $fruits = array ("apple", "orange", array ("pear", "mango"),"banana"); echo (count($fruits, 1)); ?>
a) 6
b) 5
c) 4
d) 3
View Answer
Explanation: function count() will return the number of elements in an array. The parameter 1 counts the array recursively i.e it will count all the elements of multidimensional arrays.
11. What will be the output of the following PHP program?
<?php function multi($num) { if ($num == 3) echo "I Wonder"; if ($num == 7) echo "Which One"; if ($num == 8) echo "Is The"; if ($num == 19) echo "Correct Answer"; } $can = stripos("I love php, I love php too!","PHP"); multi($can); ?>
a) Correct Answer
b) Is The
c) I Wonder
d) Which One
View Answer
Explanation: The stripos() function finds the position of the first occurrence of a string inside another string. In this case it returns 7.
12. Which of the following PHP functions can be used for generating unique ids?
a) md5()
b) uniqueid()
c) mdid()
d) id()
View Answer
Explanation: The function uniqueid() is used to generate a unique ID based on the microtime (current time in microseconds). The ID generated from the function uniqueid() is not optimal, as it is based on the system time. To generate an ID which is extremely difficult to predict we can use the md5() function.
13. In the following PHP program, what is/are the properties?
<?php class Example { public $name; function Sample() { echo "Learn PHP @ Sanfoundry"; } } ?>
a) function sample()
b) echo “This is an example”;
c) public $name;
d) class Example
View Answer
Explanation: Above code is an example of ‘classes’. Classes are the blueprints of objects. Classes are the programmer-defined data type, which includes the local methods and the local variables. Class is a collection of objects which has properties and behaviour.
14. What will be the output of the following PHP code?
<?php define("GREETING", "PHP is a scripting language"); echo $GREETING; ?>
a) $GREETING
b) no output
c) PHP is a scripting language
d) GREETING
View Answer
Explanation: Constants do not need a $ before them, they are referenced by their variable names itself.
15. A function in PHP which starts with __ (double underscore) is known as __________
a) Default Function
b) User Defined Function
c) Inbuilt Function
d) Magic Function
View Answer
Explanation: PHP functions that start with a double underscore – a “__” – are called magic functions. They are functions that are always defined inside classes, and are not stand-alone functions.
16. How many functions does PHP offer for searching and modifying strings using Perl-compatible regular expressions.
a) 10
b) 7
c) 8
d) 9
View Answer
Explanation: The functions are preg_filter(), preg_grep(), preg_match(), preg_match_all(), preg_quote(), preg_replace(), preg_replace_callback(), and preg_split().
17. Which of the following web servers are required to run the PHP script?
a) Apache and PHP
b) IIS
c) XAMPP
d) Any of the mentioned
View Answer
Explanation: To run PHP code you need to have PHP and a web server, both IIS, XAMPP and Apache are web servers. You can choose either one according to your platform.
18. What will be the output of the following PHP code snippet?
<?php $url = "[email protected]"; echo ltrim(strstr($url, "@"),"@"); ?>
a) [email protected]
b) [email protected]
c) phpmcq@
d) sanfoundry.com
View Answer
Explanation: The strstr() function returns the remainder of a string beginning with the first occurrence of a predefined string.
19. Which of the following PHP functions can be used to get the current memory usage?
a) memory_get_usage()
b) memory_get_peak_usage()
c) get_peak_usage()
d) get_usage()
View Answer
Explanation: memory_get_usage() returns the amount of memory, in bytes, that’s currently being allocated to the PHP script. We can set the parameter ‘real_usage’ to TRUE to get total memory allocated from system, including unused pages. If it is not set or FALSE then only the used memory is reported. To get the highest amount of memory used at any point, we can use the memory_get_peak_usage() function.
20. Which one of the following PHP function is used to determine a file’s last access time?
a) filetime()
b) fileatime()
c) fileltime()
d) filectime()
View Answer
Explanation: The fileatime() function returns a file’s last access time in Unix timestamp format or FALSE on error.
21. What will be the output of the following PHP code?
<?php $x = 5; $y = 10; function fun() { $y = $GLOBALS['x'] + $GLOBALS['y']; } fun(); echo $y; ?>
a) 5
b) 10
c) 15
d) Error
View Answer
Explanation: The value of global variable y does not change therefore it’ll print 10;
22. PHP recognizes constructors by the name _________
a) function __construct()
b) function _construct()
c) classname()
d) _construct()
View Answer
Explanation: PHP recognizes constructors by double underscore followed by the construct keyword. Its syntax is function __construct ([ argument1, argument2,…..]) { Class Initialization code }.
23. The developers of PHP deprecated the safe mode feature as of which PHP version?
a) PHP 5.3.1
b) PHP 5.3.0
c) PHP 5.1.0
d) PHP 5.2.0
View Answer
Explanation: This happened because safe mode often creates many problems as it resolves, largely due to the need for enterprise applications to use many of the features safe mode disables.
24. What will be the value of the variable $input in the following PHP program?
<?php $input = "PHP<td>stands for</td>Hypertext<i>Preprocessor</i>!"; $input = strip_tags($input,"<i></i>"); echo $input; ?>
a) PHP stands for Hypertext <i>Preprocessor</i>!
b) PHP stands for Hypertext Preprocessor!
c) PHP <td>stands for</td> Hypertext <i>Preprocessor</i>!
d) PHP <td>stands for</td> Hypertext Preprocessor!
View Answer
Explanation: Italic tags <i></i> might be allowable, but table tags <td></td> could potentially wreak havoc on a page.
25. Which of the following variables does PHP use to authenticate a user?
i) $_SERVER['PHP_AUTH_USER']. ii) $_SERVER['PHP_AUTH_USERS']. iii) $_SERVER['PHP_AUTH_PU']. iv) $_SERVER['PHP_AUTH_PW'].
a) ii) and iv)
b) i) and iv)
c) ii) and iii)
d) i) and ii)
View Answer
Explanation: $_SERVER[‘PHP_AUTH_USER’] and $_SERVER[‘PHP_AUTH_PW’] store the username and password values, respectively.
26. What does PDO stand for?
a) PHP Database Orientation
b) PHP Data Orientation
c) PHP Data Object
d) PHP Database Object
View Answer
Explanation: PDO stands for PHP Data Object. The PDO class provides a common interface to different database applications.
27. What will be the output of the following PHP program?
<?php $a = 100; if ($a > 10) printf("PHP Quiz"); else if ($a > 20) printf("PHP MCQ"); else if($a > 30) printf("PHP Program"); ?>
a)
PHP Quiz PHP MCQ PHP Program
b) PHP Quiz
c) No output
d) PHP MCQ
View Answer
Explanation: In if else if one condition is satisfied then no other condition is checked.
28. Which of the looping statements is/are supported by PHP?
i) for loop ii) while loop iii) do-while loop iv) foreach loop
a) Only iv)
b) i) and ii)
c) i), ii) and iii)
d) i), ii), iii) and iv)
View Answer
Explanation: All are supported looping statements in PHP as they can repeat the same block of code a given number of times, or until a certain condition is met.
29. Which PHP statement will give output as $x on the screen?
a) echo “\$x”;
b) echo “$$x”;
c) echo “/$x”;
d) echo “$x;”;
View Answer
Explanation: A backslash is used so that the dollar sign is treated as a normal string character rather than prompt PHP to treat $x as a variable. The backslash used in this manner is known as the escape character.
30. Which version of PHP introduced the advanced concepts of OOP?
a) PHP 6
b) PHP 4
c) PHP 5
d) PHP 5.3
View Answer
Explanation: Advanced concepts of OOP were introduced in PHP version 5.
31. What will be the output of the following PHP code?
<?php $x = 4; $y = 3 $z = 1; $z = $z + $x + $y; echo "$z"; ?>
a) 15
b) 8
c) 1
d) $z
View Answer
Explanation: Normal addition of variables x, y and z occurs and result of 8 will be displayed.
32. What will be the output of the following PHP program?
<?php $a = "$winner"; $b = "/$looser"; echo $a,$b; ?>
a) /
b) $looser
c) /$looser
d) $winner/$looser
View Answer
Explanation: Since variables $winner and $looser is not defined we only see / as output.
33. Which one of the following is the default PHP session name?
a) PHPSESSIONID
b) PHPIDSESS
c) PHPSESSID
d) PHPSESID
View Answer
Explanation: PHPSESSID is the default PHP session name. You can change this name by using the session.name directive.
34. What will be the output of the following PHP program?
<?php $mcq = 1; switch(print $mcq) { case 2: print "HTML"; break; case 1: print "CSS"; break; default: print "JavaScript"; } ?>
a) error
b) 1HTML
c) 1JavaScript
d) 1CSS
View Answer
Explanation: Print returns 1, thus it gives case 1.
35. What will be the output of the following PHP program?
<?php define("VAR_NAME","test"); ${VAR_NAME} = "value"; echo VAR_NAME; echo ${VAR_NAME}; ?>
a) testtest
b) testvalue
c) error, constant value cannot be changed
d) test
View Answer
Explanation: ${VAR_NAME} creates a new variable that is not same as VAR_NAME.
36. Which PHP function displays the web page’s most recent modification date?
a) getlastmod()
b) get_last_mod()
c) lastmod()
d) last_mod()
View Answer
Explanation: The function getlastmod() gets the time of the last modification of the main script of execution. It returns the value of the page’s last modified header or FALSE in the case of an error.
37. What will be the output of the following PHP program?
<?php $i = 5; while (--$i > 0 && ++$i) { print $i; } ?>
a) 555555555…infinitely
b) 54321
c) error
d) 5
View Answer
Explanation: As it is && operator it is being incremented and decremented continuously in PHP.
38. What will be the output of the following PHP code?
<?php function constant() { define("GREETING", "Welcome to Sanfoundry",true); echo greeting; } ?>
a) GREETING
b) Welcome to Sanfoundry
c) ERROR
d) greeting
View Answer
Explanation: By default, constants are case sensitive in php. But the third parameter in define(), if set to true, makes constants case insensitive.
39. Which variable is used to collect form data sent with both the GET and POST methods?
a) $_BOTH
b) $REQUEST
c) $_REQUEST
d) $BOTH
View Answer
Explanation: In PHP the global variable $_REQUEST is used to collect data after submitting an HTML form.
40. What will be the output of the following PHP program?
<?php $php = array("Array", "Function", "Strings", "File"); echo pos($php); ?>
a) Function
b) File
c) Strings
d) Array
View Answer
Explanation: The pos() function returns the value of the current element in an array, and since no operation has been done, the current element is the first element.
41. If $a = 12 what will be returned when ($a == 12) ? 5 : 1 is executed?
a) 1
b) 5
c) 12
d) Error
View Answer
Explanation: ?: is known as ternary operator. If condition is true then the part just after the ? is executed else the part after : .
Chapterwise Multiple Choice Questions on PHP
- PHP Basics
- PHP Array
- PHP OOPs
- PHP Error Handling and Exception Handling
- PHP Strings and PHP Regular Expressions
- PHP File and PHP Session Handling
- PHP Objects and Databases
- PHP Object Tools and Design
- PHP Variables
- PHP Constants
- PHP Operators
- PHP Looping Statements
- PHP Functions
1. PHP MCQ on Basics
The section contains PHP multiple choice questions and answers on basic concepts.
|
|
2. PHP MCQ on Array
The section contains PHP questions and answers on array.
|
|
3. Multiple Choice Questions on PHP OOPs
The section contains PHP multiple choice questions on basic and advanced concepts of object-oriented PHP.
|
|
4. PHP MCQ on Error Handling and Exception Handling
The section contains PHP multiple choice questions and answers on PHP filters, error and exception handling.
|
|
5. PHP MCQ on Strings and Regular Expressions
The section contains questions and answers on PHP strings and PHP regular expression concepts.
|
|
6. Multiple Choice Questions on PHP File and PHP Session Handling
The section contains PHP MCQ Questions on file system, date and timestamps, PHP forms in HTML, session handling, networking concepts and concept of web security using PHP.
7. PHP Multiple Choice Questions on Objects and Databases
The section contains PHP multiple choice questions and answers on concept of preg, basic and advanced concepts of objects in php and the concept of image uploading in PHP.
8. Multiple Choice Questions on PHP Object Tools and Design
The section contains PHP questions and answers on object tools and their design.
|
|
9. PHP MCQ on Variables
The section contains Questions and Answers on PHP variables and their syntax.
|
|
10. PHP MCQ on Constants
The section contains PHP multiple choice questions and answers on constants as well as echo and print statements.
|
|
11. Multiple Choice Questions on PHP Operators
The section contains questions and answers on PHP operators.
|
|
12. PHP MCQ on Looping Statements
The section contains PHP MCQ Questions on various looping statements like if else if, while, for and switch.
|
|
13. PHP Multiple Choice Questions on Functions
The section contains PHP multiple choice questions and answers on functions and PHP In-built functions.
|
|
Wish you the best in your endeavor to learn and master PHP!
Best Books on PHP:
PHP Online Test:
- PHP Programming Tests
- PHP Certification Test
- PHP Internship Test
- PHP Top Rankers
- PHP Practice Test – Set 1
- PHP Practice Test – Set 2
- PHP Practice Test – Set 3
- PHP Practice Test – Set 4
- PHP Practice Test – Set 5
- PHP Practice Test – Set 6
- PHP Practice Test – Set 7
- PHP Practice Test – Set 8
- PHP Practice Test – Set 9
- PHP Practice Test – Set 10
- PHP Mock Test – Set 1
- PHP Mock Test – Set 2
- PHP Mock Test – Set 3
- PHP Mock Test – Set 4
- PHP Mock Test – Set 5
- PHP Mock Test – Set 6
- PHP Mock Test – Set 7
- PHP Mock Test – Set 8
- PHP Mock Test – Set 9
- PHP Mock Test – Set 10
Important Links:
- Python Multiple Choice Questions
- HTML Multiple Choice Questions
- JavaScript Multiple Choice Questions
- Java Multiple Choice Questions
- C++ Multiple Choice Questions
- C Multiple Choice Questions
- Computer Science Multiple Choice Questions