PHP and Database Interview Questions and Answers
====================================================
OBJECT-ORIENTED PROGRAMMING (OOP) CONCEPTS IN PHP:
1. Class and Object
-------------------
Class: A blueprint for creating objects.
Object: An instance of a class.
Example:
class Car {
public $brand;
public $color;
public function startEngine() {
return "Engine started!";
$myCar = new Car(); // Creating an object
$myCar->brand = "Toyota";
$myCar->color = "Red";
echo $myCar->brand; // Output: Toyota
echo $myCar->startEngine(); // Output: Engine started!
2. Inheritance
--------------
Inheritance allows a class to inherit properties and methods from another class.
Example:
class Vehicle {
public $type;
public function move() {
return "Moving!";
class Bike extends Vehicle {
public $brand;
public function displayBrand() {
return "This is a " . $this->brand;
$bike = new Bike();
$bike->type = "Two-wheeler";
$bike->brand = "Yamaha";
echo $bike->move(); // Output: Moving!
echo $bike->displayBrand(); // Output: This is a Yamaha
... (all other OOP questions and examples included here)
====================================================
DATABASE INTERVIEW QUESTIONS:
1. What is a database?
----------------------
Answer: A database is an organized collection of data that can be easily accessed, managed, and
updated.
2. What is SQL?
---------------
Answer: SQL (Structured Query Language) is a standard language used to communicate with
relational databases to perform operations like querying, updating, inserting, and deleting data.
3. What is the difference between DDL, DML, and DCL?
-----------------------------------------------------
- DDL (Data Definition Language): Defines the structure of the database (e.g., CREATE, ALTER,
DROP).
- DML (Data Manipulation Language): Handles data manipulation (e.g., INSERT, UPDATE,
DELETE, SELECT).
- DCL (Data Control Language): Controls access to data (e.g., GRANT, REVOKE).
4. What is a primary key?
-------------------------
Answer: A primary key is a unique identifier for a record in a table. It ensures that no duplicate
values exist and cannot be NULL.
Example:
CREATE TABLE Users (
ID INT PRIMARY KEY,
Name VARCHAR(50)
);
5. What is a foreign key?
-------------------------
Answer: A foreign key is a field in one table that refers to the primary key in another table,
establishing a relationship between the two tables.
Example:
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
UserID INT,
FOREIGN KEY (UserID) REFERENCES Users(ID)
);
... (all other database questions and answers included here)