PHP interview questions for freshers usually cover the basics of the language, variables and superglobals, object-oriented programming, sessions and cookies, working with MySQL, basic security, and one or two small coding tasks. If you can answer the 20 questions in this guide in your own words, with a tiny example for each, you are ready for most first-round PHP interviews.
Do not memorise long definitions. Interviewers check whether you understand what the code does. Read each answer, say it aloud once, and type the examples yourself — that is how answers stop vanishing when someone says, "Please take a seat."
PHP Basics: Questions 1 to 4
1. What is PHP?
PHP is a server-side scripting language used mainly for building dynamic websites and web applications. The code runs on the server, creates HTML, and sends only that HTML to the browser. WordPress and thousands of business sites are built with PHP. The official PHP website at php.net is the home of the language and its documentation.
2. How do you start and end PHP code in a file?
PHP code sits between opening and closing tags, and anything outside those tags is sent as plain HTML. A file can mix both, which is why PHP is easy to add to an existing page.
<?php
echo "Hello, interviewer!";
?>Save the file with a .php extension and run it through a web server. The browser sees only the output, never your source code.
3. What is the difference between echo and print?
Both display output, but echo is slightly faster, can print several values separated by commas, and returns nothing. print prints one value and returns 1, so it can be used inside an expression. In daily work most developers use echo, and either answer with that reason will satisfy an interviewer.
4. Is PHP a loosely typed language? What are its data types?
Yes. You do not declare a variable type in advance; PHP decides from the value you assign, and the type can change later. Common types are string, integer, float, boolean, array, object, and null. This is beginner-friendly, though you should check values carefully before comparing them.
Variables and Superglobals: Questions 5 to 8
5. How do you create a variable in PHP?
Every variable name starts with a dollar sign, followed by a letter or underscore. Names are case-sensitive, so $name and $Name are different variables — a classic trap that has embarrassed everyone at least once.
$name = "Priya";
$age = 21;
echo $name;6. How do you define a constant?
Constants hold a value that should not change during the script, like a site name or a tax rate. You create one with define() or the const keyword, and you use it without a dollar sign.
define("SITE_NAME", "Coding Cush");
echo SITE_NAME;7. What is the difference between == and ===?
The == operator compares only values after converting types, so "5" == 5 is true. The === operator compares value and type, so "5" === 5 is false. Interviewers love this question because it shows whether you understand loose typing. When in doubt, prefer === for predictable results.
8. What are superglobals in PHP?
Superglobals are built-in arrays available in every scope. Freshers should know $_GET for URL data, $_POST for form data, $_SESSION for session data, $_COOKIE for cookies, $_SERVER for server information, and $_FILES for uploads.
Object-Oriented PHP: Questions 9 to 12
9. What is a class and an object?
A class is a blueprint that groups properties and methods together, and an object is a real instance created from that blueprint. Think of a class as the design of a chair and an object as an actual chair you can sit on. The PHP manual page on classes and objects explains this model in full.
class Student {
public $name;
}
$student = new Student();
$student->name = "Arjun";10. What is a constructor?
A constructor is a special method named __construct() that runs automatically when you create an object. Developers use it to set starting values, so every new object begins in a clean, ready state instead of needing extra setup code after it.
11. What do public, private, and protected mean?
These are visibility keywords. Public members can be accessed from anywhere. Private members can be used only inside their own class. Protected members can be used inside the class and its child classes. In short: public is open, private is locked, and protected is family-only.
12. What is inheritance?
Inheritance lets a child class reuse the parent class's properties and methods, using the extends keyword, and then add or change behaviour of its own. It reduces repeated code. A PremiumStudent class, for example, can inherit common student details and add only premium features.
Sessions, Cookies and Forms: Questions 13 to 16
13. What is the difference between a session and a cookie?
A cookie stores small data in the user browser, while a session stores data on the server and keeps only a session ID in a cookie. Sessions are safer for login information because the real data never leaves the server. Always call session_start() before using session values.
session_start();
$_SESSION["user"] = "Priya";14. What is the difference between GET and POST?
| Feature | GET | POST |
|---|---|---|
| Where data goes | In the URL | In the request body |
| Visible to others? | Yes, in the address bar | No, not in the URL |
| Best for | Search and page numbers | Forms, logins and passwords |
| Can be bookmarked? | Yes | No |
Short rule: use GET when reading, and POST when sending or changing something important.
15. How do you get form data in PHP?
Give each form field a name attribute, choose GET or POST as the form method, and read the matching superglobal. Always check a value exists before using it, and clean user input before displaying it — visitors can type absolutely anything into a form.
16. How do you redirect to another page in PHP?
Send a Location header before any output is printed, and follow it with exit so the rest of the script stops running. Forgetting exit is a common fresher mistake: the browser moves on, but the old script quietly keeps working behind it.
header("Location: dashboard.php");
exit;MySQL, Security and Practical Coding: Questions 17 to 20
17. How do you connect PHP with MySQL?
Modern PHP connects using MySQLi or PDO, by giving the host, username, password and database name, and checking for errors. PDO is often preferred because it works with several database types and supports prepared statements well. Our guide on how to connect PHP with MySQL database walks through the full working code.
18. How do you keep a PHP application secure?
Mention four habits: use prepared statements so user input cannot change your queries, escape output to prevent cross-site scripting, hash passwords with password_hash() instead of storing plain text, and validate every form value on the server. In short, never trust user input blindly.
19. What is an API, and can PHP build one?
An API lets two programs talk by sending requests and receiving data, usually in JSON format. PHP is widely used for this, and a small REST API needs only routing, the right headers, and json_encode() for responses. Our tutorial on how to build a simple REST API in PHP builds one from scratch.
20. Practical question: how do you reverse a string in PHP?
Expect one small task like this, a palindrome check, or printing a pattern. For a string, the built-in strrev() function is the honest first answer, and interviewers may then ask you to do it with a loop to check your logic.
$word = "php";
echo strrev($word);Practise five such tasks — reverse a string, check a palindrome, find the largest number in an array, count vowels, and swap two numbers. They repeat in interviews often.
Frequently Asked Questions
How should a fresher prepare for a PHP interview in one week?
Spend two days on basics and variables, two on OOP and forms, one on MySQL and security, and the last two typing small programs and explaining them aloud.
Do PHP interviews include coding questions?
Yes, usually one or two small ones, on strings, arrays, patterns, or a simple form. Write clean code and explain your thinking while you write.
Should I learn a PHP framework before my first interview?
Core PHP first. Once basics are solid, basic Laravel knowledge is a strong bonus, but a framework cannot hide weak fundamentals for long.
Conclusion
These 20 questions cover what most fresher PHP interviews ask: clear basics, careful variables, simple OOP, sessions and forms, safe database work, and one honest coding task. Learn the ideas, type every example yourself, and answer in short, confident sentences.
Leave a Comment