PHP - MCQ Questions and Answers

Test your knowledge of PHP with these interactive multiple-choice questions.

« First 1 2 3 4 Last »
« Previous Page Next Page »

1. What does PHP stand for?

  • a) PHP: Hypertext Preprocessor
  • b) Private Home Page
  • c) Personal Hypertext Processor
  • d) Preprocessed Hypertext Page
Answer: A - PHP originally stood for "Personal Home Page" but now stands for "PHP: Hypertext Preprocessor" (a recursive acronym).

2. Which of the following is the correct way to start a PHP block?

  • a) <?php
  • b) <?>
  • c) <script language="php">
  • d) All of the above
Answer: D - PHP code can be started with <?php (recommended), <? (short tags), or <script language="php">.

3. What is the result of echo "1" + "2";?

  • a) 3
  • b) 12
  • c) "12"
  • d) Error
Answer: A - PHP performs type juggling and converts the strings to numbers when using the + operator.

4. Which superglobal holds information about headers, paths, and script locations?

  • a) $_SERVER
  • b) $_GET
  • c) $_SESSION
  • d) $_ENV
Answer: A - $_SERVER contains server and execution environment information.

5. What is the correct way to create a constant in PHP?

  • a) define("CONSTANT", "value");
  • b) const CONSTANT = "value";
  • c) $constant = "value";
  • d) Both A and B
Answer: D - Constants can be defined with define() or the const keyword (in class definitions or at compile time).

6. Which function converts a string to lowercase?

  • a) strtolower()
  • b) strtoupper()
  • c) ucfirst()
  • d) lcfirst()
Answer: A - strtolower() converts a string to lowercase.

7. What does the === operator check?

  • a) Value and type equality
  • b) Only value equality
  • c) Only type equality
  • d) Reference equality
Answer: A - === checks both value and type (strict comparison).

8. Which function returns the length of a string?

  • a) strlen()
  • b) count()
  • c) sizeof()
  • d) length()
Answer: A - strlen() returns the length of a string in bytes.

9. What is the default session storage mechanism in PHP?

  • a) Files on the server
  • b) Browser cookies
  • c) Database
  • d) Memory
Answer: A - By default, PHP stores session data in files on the server.

10. Which function is used to redirect to another page?

  • a) header()
  • b) redirect()
  • c) location()
  • d) forward()
Answer: A - header("Location: url") is used for page redirection (must be called before any output).

11. What does PDO stand for?

  • a) PHP Data Objects
  • b) Portable Data Objects
  • c) Persistent Database Objects
  • d) Predefined Data Objects
Answer: A - PDO stands for PHP Data Objects, a database access layer.

12. Which function is used to check if a variable is an array?

  • a) is_array()
  • b) is_array()
  • c) isArray()
  • d) typeOf()
Answer: A - is_array() checks if a variable is an array.

13. What is the correct way to include a file in PHP?

  • a) include 'file.php';
  • b) require 'file.php';
  • c) include_once 'file.php';
  • d) All of the above
Answer: D - All are valid, with different behaviors on failure (include continues, require stops) and multiple inclusions.

14. Which function is used to destroy a session?

  • a) session_destroy()
  • b) session_end()
  • c) session_abort()
  • d) session_close()
Answer: A - session_destroy() removes all session data.

15. What is the correct way to create a cookie in PHP?

  • a) setcookie(name, value, expire);
  • b) create_cookie(name, value);
  • c) cookie(name, value, expire);
  • d) make_cookie(name, value);
Answer: A - setcookie() is used to create cookies in PHP.

16. Which function escapes special characters in a string for SQL queries?

  • a) mysqli_real_escape_string()
  • b) escape_string()
  • c) sql_escape()
  • d) mysql_escape()
Answer: A - mysqli_real_escape_string() escapes special characters for SQL (though prepared statements are preferred).

17. What is the correct way to create an array in PHP?

  • a) $arr = array(1, 2, 3);
  • b) $arr = [1, 2, 3];
  • c) $arr = new Array(1, 2, 3);
  • d) Both A and B
Answer: D - Both array() and [] (since PHP 5.4) are valid array syntax.

18. Which function returns the current date and time?

  • a) date()
  • b) time()
  • c) now()
  • d) getdate()
Answer: A - date() formats the current timestamp (default) or a specified timestamp.

19. What does the __construct() method do?

  • a) It's a class constructor
  • b) It destroys an object
  • c) It converts an object to a string
  • d) It creates a clone of an object
Answer: A - __construct() is called when an object is instantiated.

20. Which function is used to send email in PHP?

  • a) mail()
  • b) sendmail()
  • c) email()
  • d) smtp()
Answer: A - The mail() function is used to send email (though libraries like PHPMailer are often preferred).
« First 1 2 3 4 Last »
« Previous Page Next Page »

Learn Computer Skills with PCBooks

Master computer skills with PCBooks! Explore interactive tutorials, MCQ tests, and online exams for programming, web development, and IT fundamentals. Perfect for beginners and advanced learners.

Online MCQ Tests for Programming

Practice programming MCQ questions and improve your coding skills with our online quizzes. Whether you're learning Python, Java, or C programming, PCBooks has you covered.

Free Web Development Tutorials

Learn HTML, CSS, and JavaScript with our free web development tutorials. Test your knowledge with interactive quizzes and online exams.