1) What is PHP?
PHP (Hypertext Preprocessor) is a server-side scripting language designed for web development but also used as a general-purpose programming language.
2) What is PHP version?
PHP version refers to the release of the PHP engine you are using. For example, PHP 8.3, PHP 8.2, etc. Each version brings new features and performance improvements.
3) What are data types?
Data types define the type of value a variable can hold. PHP has the following:
- Integer → whole numbers
- Float/Double → decimal numbers
- String → text
- Boolean → true/false
- Array → collection of values
- Object → instance of class
- NULL → no value
4) What is an array?
An array is a collection of multiple values stored in a single variable.
Example:
$numbers = [1, 2, 3, 4, 5];
5) What is include and require?
Both are used to include files in PHP.
- include → gives a warning if file not found but script continues.
- require → gives a fatal error if file not found and stops script.
6) What are superglobal variables?
Superglobals are built-in variables accessible from anywhere in PHP. Examples:
- $_GET, $_POST, $_REQUEST, $_SESSION, $_COOKIE, $_SERVER, $_FILES, $_ENV
7) What does $$a mean?
$$a is a variable variable, meaning the value of $a becomes the name of another variable.
$a = “name”;
$name = “Sandip”;
echo $$a; // Output: Sandip
8) What is a variable?
A variable is a container to store data. In PHP, variables start with $.
Example: $age = 25;
9) Difference between $_GET, $_POST, $_REQUEST
| Method | Data Visibility | Size Limit | Security |
| $_GET | URL (query string) | Limited | Less secure |
| $_POST | Body of HTTP request | Large | More secure |
| $_REQUEST | Both GET + POST + COOKIE | Depends | Less predictable |
10) Sorting functions in PHP
- sort() → ascending order
- rsort() → descending order
- asort() → associative array ascending
- arsort() → associative array descending
- ksort() → sort by keys ascending
- krsort() → sort by keys descending
11) Common array functions in PHP
- array_push(), array_pop(), array_shift(), array_unshift()
- array_merge(), array_combine(), array_diff(), array_intersect()
- in_array(), count(), array_slice(), array_splice()
12) Difference between array_merge and array_combine
- array_merge() → merges multiple arrays into one.
- array_combine() → creates an array using one array as keys and another as values.
13) How to submit a form and process in PHP
<!– HTML Form –>
<form method=”post” action=”process.php”>
Name: <input type=”text” name=”name”>
<input type=”submit” value=”Submit”>
</form>
<!– process.php –>
<?php
$name = $_POST[‘name’];
echo “Hello, $name”;
?>
14) What is use for security in your project?
Security is very important in PHP projects to protect the application from unauthorized access, hacking, and data leakage.
In my projects, I use multiple security practices such as:
- Input Validation & Sanitization
To prevent SQL Injection and XSS attacks. - Prepared Statements (PDO/MySQLi)
To securely execute database queries. - Password Hashing
Using password_hash() and password_verify() instead of storing plain passwords. - Session Management
Secure login system using sessions and session timeout. - CSRF Protection
Using CSRF tokens in forms to prevent fake requests. - Authentication & Authorization
Role-based access control (Admin/User). - File Upload Security
Validating file types and limiting file size.
Example (Prepared Statement):
$stmt = $conn->prepare(“SELECT * FROM users WHERE email = ?”);
$stmt->bind_param(“s”, $email);
$stmt->execute();
This prevents SQL Injection.
Interview Answer (Short)
In my PHP project, I use security measures like input validation, prepared statements, password hashing, session handling, CSRF protection, and role-based access to protect the application from attacks like SQL Injection and XSS.
OOP in PHP
15) What is OOP?
Object-Oriented Programming (OOP) is a programming paradigm based on objects and classes.
16) What is inheritance?
Inheritance allows a class to reuse properties and methods of another class.
class ParentClass { public $name = “Parent”; }
class ChildClass extends ParentClass {}
17) What is encapsulation?
Encapsulation restricts access to class properties and methods using private or protected access modifiers.
18) Difference between interface and abstraction
| Feature | Interface | Abstract Class |
| Methods | Only abstract methods | Can have abstract & normal methods |
| Multiple | Can implement multiple | Only single inheritance |
| Properties | Cannot have properties | Can have properties |
19) What is a trait?
Trait is a reusable set of methods to include in multiple classes (PHP 5.4+).
trait Hello { function sayHi() { echo “Hi”; } }
class Greet { use Hello; }
Database Basics
20) What is a primary key?
A column that uniquely identifies each record in a table.
21) What is a foreign key?
A column that links one table to another (enforces referential integrity).
22) Difference between TRUNCATE and DELETE
| Feature | DELETE | TRUNCATE |
| Speed | Slower | Faster |
| Where clause | Allowed | Not allowed |
| Auto-increment reset | No | Yes |
23) What is indexing?
Indexing improves query performance by allowing faster search in a table.
24) What is normalization?
Normalization is organizing database tables to reduce redundancy and improve integrity.
25) How to optimize a query
- Use proper indexes
- Avoid SELECT *
- Use joins instead of subqueries
- Limit data using WHERE
- Optimize database structure
26) What is JOIN?
JOIN is used to combine rows from two or more tables. Types: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN.
27) What is DDL?
Data Definition Language (DDL) → SQL commands to define structure: CREATE, ALTER, DROP.
28) What is DML?
Data Manipulation Language (DML) → SQL commands to manipulate data: INSERT, UPDATE, DELETE.
29) What are triggers?
Triggers are automatic actions executed in response to INSERT, UPDATE, or DELETE events in a table.
PHP & SQL Coding Examples
30) Display output:
*****
****
***
**
*
for($i=5; $i>=1; $i–){
echo str_repeat(“*”, $i).”<br>”;
}
31) Output of:
$a = 5;
$b = $a++;
$c = ++$a;
echo $a; // 7
echo $b; // 5
echo $c; // 7
Explanation:
- $a++ → post-increment: use 5 then increase to 6
- ++$a → pre-increment: increase 6 to 7 then use 7
32) Find max or min from given array
$array = [‘1′,’2′,’3′,’4′,’5’];
echo max($array); // 5
echo min($array); // 1
33) Find second highest salary
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
34) Find department name for employee ID
SELECT e.emp_name, d.dept_name
FROM employees e
JOIN departments d ON e.dept_id = d.dept_id
WHERE e.emp_id = 101;
