Whether you are preparing for your university semester viva examination (BCA, BSc Computer Science, MCA, B.Tech) or getting ready for technical campus placement and web developer interviews, this comprehensive collection of 100+ Top PHP Questions and Answers covers all levels from core fundamentals to modern PHP 8 architecture.
Section 1: Core PHP Fundamentals (Freshers & BCA Viva)
PHP stands for Hypertext Preprocessor. It is an open-source, server-side interpreted scripting language designed primarily for dynamic web development.
echo has no return value (void), can accept multiple comma-separated arguments, and is slightly faster. print always returns integer 1 and accepts only one argument. Both are language constructs.
== (Equal) compares values after type coercion (e.g. 5 == "5" is true). === (Identical) checks that both value AND data type are strictly identical (e.g. 5 === "5" is false).
If the target file is missing, include generates a warning and allows the script to continue. require generates a fatal compile error and halts script execution immediately.
Superglobals are predefined associative arrays available automatically in all scopes without the global keyword. Examples: $_GET, $_POST, $_SESSION, $_COOKIE, $_SERVER, $_FILES, and $GLOBALS.
Section 2: Web Forms, Cookies & Sessions
Cookies are stored on the client's browser (max ~4KB) and are accessible across requests. Sessions store application data securely on the web server; only a session identifier (PHPSESSID) is stored in the browser cookie.
Always sanitize user output using htmlspecialchars($input, ENT_QUOTES, 'UTF-8'). This safely encodes special characters like < and > into harmless HTML entities.
session_start() sets the session cookie in HTTP response headers. Outputting HTML or echo statements first sends headers prematurely, triggering "Headers already sent" errors.
Section 3: Databases & Security (MySQLi & PDO)
SQL Injection occurs when user input is concatenated directly into SQL queries, altering command logic. Prepared statements pre-compile the SQL template on the database server; user inputs are bound purely as data parameters, making SQL injection impossible.
PDO supports 12 different database engines (MySQL, PostgreSQL, SQLite, etc.) and named parameters (:param). MySQLi connects only to MySQL and supports only positional question mark (?) placeholders.
Section 4: Object-Oriented PHP & Modern PHP 8
An Abstract Class can contain concrete methods with code and properties; a class can extend only one abstract class. An Interface contains only method signatures without bodies; a class can implement multiple interfaces.
It short-circuits nested object property or method calls: if any member in the chain evaluates to null, the entire expression safely returns null without throwing fatal null pointer exceptions.
It allows declaring properties directly in the constructor parameter list (e.g. public function __construct(public string $name) {}), eliminating separate property declarations and assignments.