Core PHP Interview Questions
PHP interviews with 50+ core PHP interview questions for experienced developers. Covers arrays, OOP, sessions, security, and real-world examples with detailed answers.
- What is PHP?
- How is PHP different from JavaScript?
- What is a Variable in PHP?
- What are Data Types in PHP?
- Difference between echo and print.?
- What is the difference between = and == and ===?
- What are Constants in PHP?
- Difference between for, while, and foreach.?
- What is a Switch Statement?
- What is break and continue?
- What is the difference between built-in and user-defined functions?
- What is $_GET and $_POST?
- Difference between $_REQUEST and $_POST
- Difference between mysqli and PDO ?
What is PHP?
PHP stands for Hypertext Preprocessor.
PHP is a server-side scripting language used to create dynamic web pages. (server side means is It runs on the server not in the browser)
How is PHP different from JavaScript?
Execution
PHP → Server-Side Language
-
PHP runs on the web server
-
The browser receives only the final HTML output
-
Users cannot see PHP code
- PHP is used for Database operations, Authentication (login systems), Sessions & cookies, REST API development
Browser → Request → Server → PHP executes → HTML sent to browser
JavaScript → Client-Side Language
-
JavaScript runs in the browser
-
It directly manipulates the DOM
-
Code is visible to the user
- JavaScript is used for Handling button clicks & events, Form validation (without page reload), Animations, DOM manipulation
Browser → JS executes → UI updates dynamically
Interview line:
PHP is a server-side scripting language, while JavaScript is primarily a client-side scripting language.
What is a Variable in PHP?
A variable in PHP is a container used to store data (value), such as:
-
numbers, strings, arrays, objects, boolean values
The value of a variable can change during program execution.
Note: In PHP, a variable always starts with $ (eg : $a = 10; or $name = "abc";)
What are Data Types in PHP?
A data type defines the type of value a variable can store.
Since PHP is a loosely typed language, you don’t need to declare the data type manually. PHP automatically detects it.
Types of Data Types in PHP:
- Scalar Data Types (String, Integer, Float, Boolean)
- Compound Data Types (Array,Object)
- Special Data Types (NULL)
Difference between echo and print.?
Both echo and print are used to display output in PHP.
Interview-Ready Answer :
echo and print are PHP language constructs used to display output. echo does not return any value and supports multiple parameters, making it slightly faster, while print returns 1 and can be used in expressions but accepts only one argument.
What is the difference between = and == and ===?
= : (Assignment operator) Assigns value to a variable
== : (Loose comparison) Compares value only (type conversion allowed)
=== : (Strict comparison) Compares value + data type
Interview-Ready Answer :
= is an assignment operator, == compares only values with type conversion, and === compares both value and data type without conversion.
What are Constants in PHP?
A constant is a name that holds a fixed value which cannot be changed once it is defined.
example : define("SITE_NAME", "InterviewReady");
echo SITE_NAME;
Difference Between Variable and Constant-
Variable : $name = "Mayank" (value can be change)
Constant : define("NAME", "Mayank") (value cannot be change)
Difference between for, while, and foreach.?
1. for Loop :
Used when you know how many times the loop should run.
for (initialization; condition; increment/decrement) {
// code
}
//////////////////////////////////////
for ($i = 1; $i <= 5; $i++) {
echo $i;
}
2. while Loop :
Runs as long as the condition is true.
while (condition) {
// code
}
//////////////////////////
$i = 1;
while ($i <= 5) {
echo $i;
$i++;
}
3. foreach Loop :
foreach ($array as $value) {
// code
}
////////////////////////////
$colors = ["red", "green", "blue"];
foreach ($colors as $color) {
echo $color;
}
What is a Switch Statement?
A switch statement is used to execute different blocks of code based on different values of a variable.
It is an alternative to multiple if...elseif...else conditions.
switch (variable) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code if no case matches
}
What is break and continue?
break-
The break statement is used to terminate the loop or switch immediately.
👉 It stops execution and comes out of the loop.
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
break;
}
echo $i;
}
/////////////////////
output :
12
- Loop stops when $i == 3
continue-
The continue statement is used to skip the current iteration and move to the next iteration of the loop.
- Loop does NOT stop
- Only skips one cycle
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue;
}
echo $i;
}
/////////////////////
Output :
1245
Note : 3 is skipped but loop continues
What is the difference between built-in and user-defined functions?
What is a Function?
A function is a block of reusable code that performs a specific task.
Types of Functions in PHP
-
Built-in functions (predefined by PHP)
-
User-defined functions (created by the developer)
Built-in Functions :
These are predefined functions provided by PHP. You don’t need to create them. (eg. strlen(), count(), date() etc.)
User-Defined Functions :
These are functions created by the programmer to perform custom tasks. (eg. function functionName() {// code} )
What is $_GET and $_POST?
$_GET and $_POST are superglobal arrays in PHP used to collect form data.
They are used to send data from the client (browser) to the server.
$_POST -
$_POST is used to collect data sent using the HTTP POST method.
important . Data is sent in the request body, not in the URL.
$_GET -
$_GET is used to collect data sent using the HTTP GET method.
Important : Data is sent through the URL
Difference between $_REQUEST and $_POST
$_POST is a superglobal array that contains data sent using the HTTP POST method.
- Data comes only from POST
- Used for form submissions
- More secure than GET/REQUEST
- Data is sent in the request body (not visible in URL)
$name = $_POST['name'];
$_REQUEST is a superglobal array that contains data from:
$_GET + $_POST + $_COOKIE
$name = $_REQUEST['name'];
it may come from: URL, Form, Cookie
Difference between mysqli and PDO ?
MySQLi (MySQL Improved) is a PHP extension used to connect only with MySQL databases.
It supports:
- OOP style
- Procedural style
- Prepared statements
PDO (PHP Data Objects) is a database-independent extension.
It supports multiple databases:
- MySQL
- PostgreSQL
- SQLite
- Oracle
- SQL Server