PHP - MCQ Questions and Answers

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

« First 3 4 5 6 Last »
« Previous Page Next Page »

61. Which function securely hashes passwords in PHP?

  • a) password_hash()
  • b) md5()
  • c) sha1()
  • d) crypt()
Answer: A - password_hash() uses bcrypt by default and is the recommended way to hash passwords.

62. How do you prevent SQL injection in PHP?

  • a) Use prepared statements (PDO/MySQLi)
  • b) Escape strings with mysql_real_escape_string()
  • c) Both A and B
  • d) Disable magic quotes
Answer: A - Prepared statements with parameterized queries are the most secure approach.

63. Which Laravel command creates a new controller?

  • a) php artisan make:controller
  • b) laravel create:controller
  • c) php make controller
  • d) artisan new:controller
Answer: A - Laravel's Artisan CLI uses make:controller.

64. What is CSRF protection in Laravel?

  • a) A security feature against cross-site request forgery
  • b) Database encryption
  • c) Password hashing
  • d) SQL injection prevention
Answer: A - CSRF tokens verify that requests originate from your application.

65. Which Symfony component is used for HTTP requests/responses?

  • a) HttpFoundation
  • b) HttpKernel
  • c) HttpClient
  • d) BrowserKit
Answer: A - HttpFoundation provides Request and Response objects.

66. How do you enable error reporting in PHP?

  • a) error_reporting(E_ALL); ini_set('display_errors', 1);
  • b) display_errors(1);
  • c) set_error_reporting('all');
  • d) debug_mode(true);
Answer: A - error_reporting() sets the level, while ini_set() enables display.

67. Which WordPress function loads the header template?

  • a) get_header()
  • b) load_header()
  • c) header_template()
  • d) include_header()
Answer: A - get_header() includes header.php from the theme.

68. What is Composer in PHP?

  • a) A dependency management tool
  • b) A framework
  • c) An IDE
  • d) A testing library
Answer: A - Composer manages PHP package dependencies.

69. Which function sanitizes user input for HTML output?

  • a) htmlspecialchars()
  • b) sanitize()
  • c) clean_input()
  • d) filter_html()
Answer: A - htmlspecialchars() converts special characters to HTML entities.

70. What is a PHP trait?

  • a) A mechanism for code reuse in single inheritance
  • b>An interface
  • c) A type of class
  • d) A namespace
Answer: A - Traits enable horizontal code reuse (unlike vertical inheritance).

71. Which PHP extension is used for MySQLi?

  • a) mysqli
  • b) mysql
  • c) pdo_mysql
  • d) sqlite
Answer: A - The mysqli extension provides improved MySQL functionality.

72. How do you implement autoloading in PHP?

  • a) spl_autoload_register()
  • b) __autoload()
  • c) auto_include()
  • d) Both A and B
Answer: D - Both work, but spl_autoload_register() is preferred (allows multiple autoloaders).

73. Which Laravel feature provides database query building?

  • a) Eloquent ORM
  • b) Query Builder
  • c) DB Facade
  • d>All of the above
Answer: D - Laravel offers multiple database interaction methods.

74. What is PHP-FPM?

  • a) FastCGI Process Manager
  • b) A framework
  • c>Package manager
  • d) Debugging tool
Answer: A - PHP-FPM manages PHP processes for high-performance sites.

75. Which WordPress hook runs during plugin activation?

  • a) register_activation_hook()
  • b) on_activate()
  • c) init_hook()
  • d) plugin_activate()
Answer: A - register_activation_hook() sets up activation routines.

76. What is the purpose of PHP's filter_var() function?

  • a) Validates and sanitizes data
  • b) Filters arrays
  • c) Creates database filters
  • d) Manages file uploads
Answer: A - filter_var() can validate emails, URLs, etc., and sanitize input.

77. Which Symfony command clears the cache?

  • a) php bin/console cache:clear
  • b) symfony cache:clean
  • c) console cache:purge
  • d) app/console cache:clear
Answer: A - Symfony 4+ uses the bin/console path for CLI commands.

78. What is Dependency Injection in PHP?

  • a) Providing dependencies to a class externally
  • b>Injecting SQL queries
  • c) A security vulnerability
  • d>PHP configuration setting
Answer: A - DI is a design pattern for loose coupling and testability.

79. Which PHP function generates a random string?

  • a) random_bytes()
  • b) rand()
  • c) mt_rand()
  • d) uniqid()
Answer: A - random_bytes() is cryptographically secure (PHP 7+).

80. What is the purpose of PHP's __invoke() method?

  • a) Allows an object to be called as a function
  • b>Destructor method
  • c>Initializes static properties
  • d>Handles method overloading
Answer: A - __invoke() enables callable objects (e.g., $object()).
« First 3 4 5 6 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.