Here are 100 real-time PHP interview questions and answers, categorized into Basic, Core, OOP, Database, Security, and Advanced sections. These are ideal for freshers, mid-level, and senior developers.
π’ BASIC PHP (1β20)
- What is PHP?
PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language. - What does PHP stand for?
PHP: Hypertext Preprocessor. - Is PHP case-sensitive?
Partially. Variables are case-sensitive; functions are not. - How do you declare a variable in PHP?
Using the$
sign, like$name = "John";
. - Difference between
echo
andprint
?echo
is slightly faster and can take multiple parameters;print
returns 1. - What are the different data types in PHP?
String, Integer, Float, Boolean, Array, Object, NULL, Resource. - What is a constant?
A name or identifier for a simple value that cannot change during script execution. Define usingdefine()
. - What are the types of arrays in PHP?
Indexed, Associative, and Multidimensional. - How to find the length of a string?
strlen($string);
- How to count array elements?
count($array);
- How do you include files in PHP?
Usinginclude
,include_once
,require
, orrequire_once
. - Difference between
include()
andrequire()
?require()
causes a fatal error on failure;include()
only a warning. - What are Superglobals?
Predefined variables like$_GET
,$_POST
,$_REQUEST
,$_SESSION
, etc. - What is the difference between GET and POST?
GET displays data in URL; POST hides it. - How to connect to MySQL using PHP?
Usingmysqli_connect()
or PDO. - What is
isset()
?
Checks if a variable is set and not null. - What is
empty()
?
Checks whether a variable is empty. - What is
die()
?
Terminates the script and outputs a message. - What is the use of
header()
?
Sends raw HTTP headers (e.g., redirects). - What is a session in PHP?
A way to store data across multiple pages using$_SESSION
.
π΅ CORE PHP (21β50)
- How to start and end PHP code?
<?php ... ?>
- What is the default file extension of PHP?
.php
- What is the difference between
==
and===
?==
checks value;===
checks value and type. - What is a constructor?
Special function called automatically when an object is created. - What is a destructor?
Special function called when an object is destroyed. - What is a magic method in PHP?
Methods starting with__
, like__construct()
,__get()
,__set()
. - What is the use of
explode()
?
Splits a string into an array. - What is
implode()
?
Joins array elements into a string. - What is a cookie?
Data stored on the clientβs browser. - How to set a cookie?
setcookie(name, value, expire);
- How to delete a cookie?
Set its expiry time in the past. - Difference between
mysql
andmysqli
?mysql
is deprecated;mysqli
is improved with more functionality. - What is PDO?
PHP Data Objects β a database access abstraction layer. - What is the use of
require_once
?
Includes a file only once to avoid duplication. - What is error handling in PHP?
Usingtry
,catch
, andfinally
blocks. - How to send email in PHP?
Using themail()
function. - What is a resource in PHP?
A special variable holding a reference to an external resource (e.g., database). - What is
nl2br()
?
Inserts HTML line breaks before newlines in a string. - How to redirect a page in PHP?
Usingheader("Location: url");
- What is
array_merge()
?
Merges two or more arrays. - What is the ternary operator?
A shorthand for if-else:$x = ($a > $b) ? $a : $b;
- What is the difference between
foreach
andfor
?foreach
is used for arrays;for
is used for loops with counters. - How to suppress errors in PHP?
Using@
before a function. - How to define a function in PHP?
function functionName() { }
- What is recursion?
A function calling itself. - What is type hinting?
Specifying expected data types for function arguments. - What is
require_once
used for?
Ensures a file is included only once. - How to create a class in PHP?
class ClassName { }
- How to create an object?
$obj = new ClassName();
- What is
this
keyword?
Refers to the current object inside a class.
π PHP & DATABASE (51β70)
- What is SQL Injection?
A security vulnerability when user input is improperly handled in SQL queries. - How to prevent SQL Injection?
Use prepared statements withmysqli
or PDO. - How to insert data into MySQL with PHP?
UsingINSERT INTO
query viamysqli_query()
. - How to fetch data from a table?
UseSELECT
query and loop withmysqli_fetch_assoc()
. - How to update a record in MySQL?
UseUPDATE
query. - How to delete a record?
UseDELETE
query. - How to check if a table exists in PHP?
UseSHOW TABLES LIKE 'tablename'
. - How to connect using PDO?
$pdo = new PDO('mysql:host=localhost;dbname=test', 'user', 'pass');
- How to handle DB errors in PDO?
Usetry/catch
block and set error mode. - How to fetch rows in PDO?
$stmt->fetch(PDO::FETCH_ASSOC);
- What are stored procedures?
Predefined SQL code saved in the database. - What is
mysqli_real_escape_string()
?
Escapes special characters to prevent SQL injection. - Difference between
fetch_assoc()
andfetch_row()
?assoc
returns associative array;row
returns numeric array. - How to count rows from a result?
mysqli_num_rows($result);
- How to close a DB connection?
mysqli_close($conn);
- What is auto_increment?
Automatically increases numeric ID on new row. - What is a primary key?
Uniquely identifies each row. - What is foreign key?
Links one table to another. - Difference between inner join and left join?
Inner join matches only common records; left join returns all left records. - What is indexing in databases?
Used to speed up data retrieval.
π£ OOP in PHP (71β90)
- What is OOP?
Object-Oriented Programming; organizes code using classes and objects. - Pillars of OOP?
Encapsulation, Inheritance, Polymorphism, Abstraction. - What is inheritance?
A class can inherit properties and methods from another class. - What is polymorphism?
Ability to call the same method on different objects with different outcomes. - What is encapsulation?
Wrapping data and methods together, restricting access using access modifiers. - What is abstraction?
Hiding implementation details and showing only the interface. - What are access modifiers?
public, private, protected. - What are interfaces?
A blueprint for classesβonly method declarations, no body. - What are traits?
Code reusability tool to include methods in multiple classes. - What is static keyword?
Declares class properties or methods that can be accessed without creating an object. - Can PHP support multiple inheritance?
No. But traits can help simulate it. - Difference between abstract class and interface?
Abstract class can have both implemented and unimplemented methods; interfaces cannot. - What is
final
keyword?
Prevents class/method from being extended/overridden. - What is a constructor chaining?
Calling a parent constructor from child usingparent::__construct()
. - What is method overloading in PHP?
PHP does not support traditional overloading; can be done using__call()
. - What is method overriding?
Redefining a parent class method in a child class. - What is object cloning?
Creating a copy using theclone
keyword. - What is namespace in PHP?
Helps avoid name conflicts in larger applications. - What is autoloading in PHP?
Automatically includes class files when used. - What is Composer?
Dependency manager for PHP.
π΄ Advanced & Security (91β100)
- What is CSRF?
Cross-Site Request Forgery β a type of malicious exploit. - How to prevent CSRF?
Use tokens in forms. - What is XSS?
Cross-Site Scripting β injecting malicious scripts. - How to prevent XSS?
Sanitize user input usinghtmlspecialchars()
orstrip_tags()
. - What are PHP design patterns?
Solutions to common software design problems (e.g., Singleton, Factory). - What is
curl
in PHP?
Library for sending HTTP requests. - How to validate email in PHP?
filter_var($email, FILTER_VALIDATE_EMAIL)
- What is
.htaccess
?
Configuration file to control Apache server settings. - How to increase script execution time?
set_time_limit(seconds);
- How to handle large file uploads?
Modifyphp.ini
:upload_max_filesize
,post_max_size
,max_execution_time
.
Restricted download
×
2 comments
[…] Top 100 PHP Interview Questions and Answers […]
[…] Top 100 PHP Interview Questions and Answers […]