Mastering the PHP & MySQL Technical Interview
PHP powers nearly 77% of the top 10 million websites. With modern PHP 8.x innovations, interviewers test your understanding of object-oriented design, security best practices, and relational database tuning.
Q1. Why should you always use PDO Prepared Statements instead of query concatenation?
Answer: Prepared statements separate the SQL template from user data. When using parameterized placeholders (`:param` or `?`), the database compiles the query structure first. Even if an attacker passes malicious characters like `' OR 1=1 --`, it is treated strictly as a string literal, completely neutralizing SQL Injection attacks.
Q2. What is the difference between `password_hash()` and `md5()`?
Answer: `md5()` is a fast, broken cryptographic hash that can be reverse-engineered in milliseconds using precomputed rainbow tables. `password_hash()` uses adaptive, computationally intensive hashing algorithms like Bcrypt or Argon2id with automatically generated secure salts and adjustable work factor cost.
Q3. Explain Database Indexing and how B-Trees optimize SELECT queries.
Answer: Without an index, MySQL must perform a Full Table Scan (evaluating every single row, O(N) complexity). A B-Tree index maintains a balanced sorted tree of key values and row pointers, reducing search complexity to O(log N). Indexes should be added to columns frequently used in `WHERE`, `JOIN`, `ORDER BY`, and `GROUP BY` clauses.
Q4. What are PHP 8 Attributes and Constructor Property Promotion?
Answer: PHP 8 introduced structured native metadata via Attributes (replacing phpdoc annotations). Constructor property promotion allows defining visibility, types, and assigning class properties directly inside the `__construct()` signature, eliminating boilerplate assignments.