PHP Interview Questions and Answers for Freshers
In this lesson: Most-asked PHP interview questions for freshers with short answers and code: sessions, == vs ===, PDO, OOP, security. Prepare fast.
How to use this list
These are the questions that come up most often in PHP interviews for freshers. For each, understand the idea and be ready to give a short, clear answer with a one-line example. Do not just memorise — knowing why matters far more to interviewers.
Basics
Q1. What is PHP? A server-side scripting language for building dynamic websites; it runs on the server and sends HTML to the browser.
Q2. echo vs print? Both output text; echo can take several values and is slightly faster, print takes one and returns 1.
Q3. == vs ===? == compares value only; === compares value and type. Prefer ===.
Q4. GET vs POST? GET puts data in the URL (searches); POST puts it in the body (logins, private data).
Q5. include vs require? Missing file: include warns and continues, require gives a fatal error and stops.
Strings and arrays
Q6. How to join strings? With the dot operator: "a" . "b".
Q7. Indexed vs associative array? Indexed uses numeric keys from 0; associative uses named keys like $a["name"].
Q8. array_map vs array_filter? map transforms every item; filter keeps only items that pass a test.
Q9. How to loop an array? With foreach, using $key => $value for pairs.
Q10. explode vs implode? explode splits a string into an array; implode joins an array into a string.
OOP
Q11. Class vs object? A class is a blueprint; an object is a real thing made from it with new.
Q12. What is a constructor? __construct() runs automatically when an object is created, used for setup.
Q13. What is inheritance? A child class extends a parent to reuse its properties and methods.
Q14. public, private, protected? Access levels: public anywhere, private same class only, protected class and children.
Q15. Abstract class vs interface? Abstract can have some code and you extend one; an interface is method names only and you can implement many.
Database and security
Q16. PDO vs MySQLi? Both connect to MySQL; PDO works with many databases and supports named placeholders.
Q17. How to prevent SQL injection? Use prepared statements with bound parameters — never paste input into SQL.
Q18. How to store passwords? Hash with password_hash, verify with password_verify; never store plain text.
Q19. What is XSS and how to stop it? Injecting HTML/JS via input; escape output with htmlspecialchars.
Q20. Session vs cookie? Session stores data on the server (safe for login); cookie stores it in the browser.
Interview tips
- Explain the why. Say not just what a feature does, but when and why you use it.
- Give a one-line example. A tiny code snippet shows real understanding.
- Mention security. Bringing up prepared statements and hashing leaves a strong impression.
- Be honest. If you do not know, say how you would find out — that is a good sign too.
Summary
- Know the core comparisons:
==vs===, GET vs POST, include vs require. - Be comfortable with arrays, strings and their common functions.
- Explain OOP clearly: class, object, constructor, inheritance, visibility.
- Always mention prepared statements, password hashing and XSS for security.
- Answer with the "why" plus a short example, not just definitions.
Revise the topics in PHP OOP and PHP MySQL connection before your interview.
Frequently Asked Questions
What is the difference between == and === in PHP?
== compares values only and ignores type, so "5" == 5 is true. === compares value and type, so "5" === 5 is false. Prefer === to avoid surprises.What is the difference between GET and POST?
What is the difference between include and require?
include gives a warning and continues, while require gives a fatal error and stops. Use require for essential files.How do you prevent SQL injection in PHP?
How are passwords stored securely in PHP?
password_hash() to store a hash and password_verify() to check at login. The database holds only the hash.