PHP MCQ (Multiple Choice Questions)

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

Answer: d
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

Answer: b
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

Answer: c
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

Answer: d
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

Answer: d
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.
advertisement
advertisement

6. Which of the following is the default file extension of PHP files?
a) .php
b) .ph
c) .xml
d) .html
View Answer

Answer: a
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

Answer: c
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

Answer: c
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

Answer: b
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

Answer: a
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.
advertisement

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

Answer: d
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

Answer: b
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?

advertisement
<?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

Answer: c
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

Answer: b
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

Answer: d
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

Answer: c
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

Answer: b
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

Answer: d
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

Answer: a
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

Answer: b
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

Answer: b
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

Answer: a
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

Answer: b
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

Answer: a
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

Answer: b
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

Answer: c
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

Answer: b
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

Answer: d
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

Answer: a
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

Answer: c
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

Answer: b
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

Answer: a
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

Answer: c
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

Answer: d
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

Answer: b
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

Answer: a
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

Answer: a
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

Answer: b
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

Answer: c
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

Answer: d
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

Answer: b
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 MCQ - Multiple Choice Questions and Answers

Our 1000+ MCQs focus on all topics of the PHP subject, covering 100+ topics. This will help you to prepare for exams, contests, online tests, quizzes, viva-voce, interviews, and certifications. You can practice these MCQs chapter by chapter starting from the 1st chapter or you can jump to any chapter of your choice.
  1. PHP Basics
  2. PHP Array
  3. PHP OOPs
  4. PHP Error Handling and Exception Handling
  5. PHP Strings and PHP Regular Expressions
  6. PHP File and PHP Session Handling
  7. PHP Objects and Databases
  8. PHP Object Tools and Design
  9. PHP Variables
  10. PHP Constants
  11. PHP Operators
  12. PHP Looping Statements
  13. PHP Functions

1. PHP MCQ on Basics

The section contains PHP multiple choice questions and answers on basic concepts.

  • PHP Basics – Set 1
  • PHP Basics – Set 2
  • PHP Basics – Set 3
  • 2. PHP MCQ on Array

    The section contains PHP questions and answers on array.

  • PHP Array – Set 1
  • PHP Array – Set 2
  • PHP Array – Set 3
  • PHP Array – Set 4
  • PHP Array – Set 5
  • 3. Multiple Choice Questions on PHP OOPs

    The section contains PHP multiple choice questions on basic and advanced concepts of object-oriented PHP.

  • PHP OOPs Basics – Set 1
  • PHP OOPs Basics – Set 2
  • PHP OOPs Advanced
  • 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.

  • PHP Error Handling
  • PHP Exception Handling
  • PHP Filter
  • 5. PHP MCQ on Strings and Regular Expressions

    The section contains questions and answers on PHP strings and PHP regular expression concepts.

  • PHP Strings & Regular Expressions – Set 1
  • PHP Strings & Regular Expressions – Set 2
  • 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.

  • PHP File System
  • PHP Date and Timestamp
  • PHP Dates and Time
  • PHP HTML Forms
  • PHP User Authentication
  • PHP Files Uploading
  • PHP Networking
  • PHP Session Handling – Set 1
  • PHP Session Handling – Set 2
  • PHP Website Security
  • 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.

  • PHP Preg Basics
  • PHP Databases – Set 1
  • PHP Databases – Set 2
  • PHP Object Basics – Set 1
  • PHP Object Basics – Set 2
  • Updating and Deleting Entries in Database using PHP
  • Image Uploading Ability
  • PHP Object Advanced Features – Set 1
  • PHP Object Advanced Features – Set 2
  • PHP Object Advanced Features – Set 3
  • 8. Multiple Choice Questions on PHP Object Tools and Design

    The section contains PHP questions and answers on object tools and their design.

  • PHP Object Tools – Set 1
  • PHP Object Tools – Set 2
  • PHP Object and Design – Set 1
  • PHP Object and Design – Set 2
  • 9. PHP MCQ on Variables

    The section contains Questions and Answers on PHP variables and their syntax.

  • PHP Syntax – Set 1
  • PHP Syntax – Set 2
  • PHP Variables – Set 1
  • PHP Variables – Set 2
  • PHP Variables – Set 3
  • PHP Variables – Set 4
  • 10. PHP MCQ on Constants

    The section contains PHP multiple choice questions and answers on constants as well as echo and print statements.

  • PHP Echo
  • PHP Print
  • PHP Constants
  • 11. Multiple Choice Questions on PHP Operators

    The section contains questions and answers on PHP operators.

  • PHP Operators – Set 1
  • PHP Operators – Set 2
  • PHP Operators – Set 3
  • PHP Operators – Set 4
  • PHP Operators – Set 5
  • 12. PHP MCQ on Looping Statements

    The section contains PHP MCQ Questions on various looping statements like if else if, while, for and switch.

  • PHP If-Else-If – Set 1
  • PHP If-Else-If – Set 2
  • PHP If-Else-If – Set 3
  • PHP Switch
  • PHP While Loop – Set 1
  • PHP While Loop – Set 2
  • PHP For Loop – Set 1
  • PHP For Loop – Set 2
  • PHP For Loop – Set 3
  • 13. PHP Multiple Choice Questions on Functions

    The section contains PHP multiple choice questions and answers on functions and PHP In-built functions.

  • PHP Function Basics
  • PHP In-Built Functions
  • PHP Functions – Set 1
  • PHP Functions – Set 2
  • PHP Functions – Set 3
  • PHP Functions – Set 4
  • PHP Functions – Set 5
  • PHP Functions – Set 6
  • If you would like to learn "PHP" thoroughly, you should attempt to work on the complete set of 1000+ MCQs - multiple choice questions and answers mentioned above. It will immensely help anyone trying to crack an exam or an interview.

    Wish you the best in your endeavor to learn and master PHP!

    Best Books on PHP:

    PHP Online Test:

    Important Links:

    advertisement
    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.