Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 18

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.

💡 Tip: for every answer, mention the "why" and a tiny example. That is what separates a strong answer from a memorised one.

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?
GET sends data in the URL and suits searches and shareable links. POST sends data in the request body and suits sensitive or large data like logins. Use POST for anything private.
What is the difference between include and require?
Both load a file. If the file is missing, 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?
Use prepared statements with PDO or MySQLi, passing user values as bound parameters instead of pasting them into the SQL. This keeps input as data, never as commands.
How are passwords stored securely in PHP?
Never as plain text. Use password_hash() to store a hash and password_verify() to check at login. The database holds only the hash.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topic50 PHP Practice Programs Your progress: 18 / 50 (36%) · Lesson 19 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free