Top 100 PHP Concept Question
Table Of Content
Questions
- What are magic methods? Tell me 3 magic methods.
- PHP support multiple inheritance?
- Interface vs abstract class, tell me the difference
- What the final keyword does?
- What does the following code?
- What are namespaces and how to use them?
- Explain what are closures?
- How to handle Error in PHP
- Explain SOLID with examples
- Explain Dependency Injection
- Composition vs Inheritance
- Abstract Classes vs Interfaces
- Traits in PHP
- Dependency Injection
- Singleton Design Pattern
- Factory Design Pattern
- Observer Pattern in PHP
- Strategy Pattern in PHP
- Builder Pattern
- Repository Pattern
- Service Container in Laravel
- Service Provider in Laravel
- Middleware in Laravel
- PHP Namespaces
- Magic Methods in PHP
- PHP Generators
- Closures and Anonymous Functions
- PHP Reflection API
- PSR Standards in PHP
- Composer and Autoloading
- Error Handling in PHP
- Exception Handling in PHP
- Handling Fatal Errors
- PHP Sessions and Cookies
- Handling File Uploads
- PHP Filters and Data Sanitization
- Cross-Site Scripting (XSS) Prevention
- SQL Injection Prevention
- Cross-Site Request Forgery (CSRF) Protection
- JWT Authentication in PHP
- OAuth Implementation in PHP
- Rate Limiting in PHP
- Microservices in PHP
- Queues and Job Processing
- Event-Driven Architecture in PHP
- GraphQL with PHP
- WebSockets in PHP
- RabbitMQ/Kafka with PHP
- Caching Strategies in PHP
- Redis vs Memcached
- PHP Performance Optimization
- Profiling PHP Code
- Asynchronous Processing in PHP
- Multithreading in PHP
- Working with cURL in PHP
- PHP Streams
- SOAP vs REST in PHP
- Building RESTful APIs
- API Versioning in PHP
- Unit Testing in PHP
- PHP Testing Frameworks
- Mocking in PHP Tests
- Continuous Integration in PHP
- Docker for PHP Development
- Scaling PHP Applications
- Horizontal vs Vertical Scaling
- CDN Usage in PHP
- Database Indexing
- Database Normalization vs Denormalization
- Optimizing SQL Queries in PHP
- Eloquent ORM vs Raw Queries
- Lazy Loading vs Eager Loading in Laravel
- Using Redis for Caching in Laravel
- Event Listeners in Laravel
- Handling Webhooks in PHP
- Logging Strategies in PHP
- Monitoring PHP Applications
- Understanding PHP OPCache
- PHP Fiber
- What’s New in PHP 8+
- Named Arguments in PHP
- Match Expression in PHP
- Union Types in PHP
- PHP Weak Maps
- Attributes in PHP
- JIT Compilation in PHP
- Fiber in PHP 8.1
- Enums in PHP 8.1
- Readonly Properties in PHP 8.1
- Final Class Constants
- PHP Dealing with Large Files
- Handling Background Jobs
- PHP Security Best Practices
- Database Transactions in PHP
- Event Sourcing in PHP
- Working with PDF in PHP
- Generating Excel Files
- Sending Emails in PHP
- PHP with Elasticsearch
- PHP Code Review Best Practices
- PHP with RabbitMQ
- Using Elasticsearch with Laravel
- GraphQL vs REST API
- Building a Multi-Tenant System in PHP
- Automating Deployment for PHP Applications
- Understanding DDD (Domain-Driven Design) in PHP
- CQRS (Command Query Responsibility Segregation) in PHP
- Microservices vs Monolith in PHP
- PHP and Message Brokers
- Serverless PHP Applications
- Best Practices for PHP Code Documentation
1. What are magic methods? Tell me 3 magic methods.
PHP provides a number of ‘magic’ methods that allow you to do some pretty neat tricks in object-oriented programming. These methods, identified by a two underscore prefix (__), function as interceptors that are automatically called when certain conditions are met.
__toString()
The __toString() method allows a class to decide how it will react when it is treated like a string. This allows and object to be converted into a string when it is treated like a string.
Example
class User {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function __toString() {
return "User Name: " . $this->name;
}
}
$user = new User("Manish");
echo $user; // Automatically calls __toString()
Output
User Name: Manish
__call()
This method is triggered when you try to reach a method what is not in the class. So, it triggers when invoking inaccessible or undefined methods on an object.
Example
class Demo {
public function __call($name, $arguments) {
return "Method '$name' does not exist. Arguments: " . implode(", ", $arguments);
}
}
$obj = new Demo();
echo $obj->undefinedMethod("arg1", "arg2");
Output
Method 'undefinedMethod' does not exist. Arguments: arg1, arg2
__get()
This behaves the same as __call() but this one is for class properties, not methods. The __get() magic method is called when accessing an inaccessible or non-existent property.
Example
class Person {
private $data = ["name" => "John", "age" => 25];
public function __get($property) {
return isset($this->data[$property]) ? $this->data[$property] : "Property '$property' not found!";
}
}
$p = new Person();
echo $p->name . "\n"; // Accesses 'name' from $data
echo $p->city; // Accesses a non-existent property
Output
John
Property 'city' not found!
2. PHP support multiple inheritance?
Multiple inheritance is an object-oriented programming (OOP) feature where a class can inherit properties and methods from more than one parent class. This allows a child class to have characteristics from multiple sources. PHP do not support multiple inheritance. A class cannot inherit from multiple classes. However, PHP provides two alternative approaches to achieve similar functionality:
- Using Interfaces
- Using Traits
Using Interfaces
PHP allows a class to implement multiple interfaces, providing a way to achieve multiple inheritance behavior.
Example:
interface Logger {
public function log($message);
}
interface Database {
public function connect();
}
class MySQL implements Logger, Database {
public function log($message) {
echo "Log: $message\n";
}
public function connect() {
echo "Connected to MySQL Database\n";
}
}
$db = new MySQL();
$db->log("Database initialized");
$db->connect();
Output:
Log: Database initialized
Connected to MySQL Database
Using Traits
Traits allow code reuse across multiple classes without requiring inheritance.
Example:
trait Logger {
public function log($message) {
echo "Log: $message\n";
}
}
trait Database {
public function connect() {
echo "Connected to Database\n";
}
}
class MySQL {
use Logger, Database; // Using multiple traits
}
$db = new MySQL();
$db->log("Database started");
$db->connect();
Output:
Log: Database started
Connected to Database
Key Differences:
Feature | Interfaces | Traits |
---|---|---|
Can have method implementation | ❌ No | ✅ Yes |
Can have properties | ❌ No | ✅ Yes |
Supports multiple inclusion | ✅ Yes | ✅ Yes |
Used for | Structure & contract enforcement | Code reusability |
Conclusion
- PHP does not support multiple inheritance directly.
- Use interfaces when enforcing method signatures across multiple classes.
- Use traits to reuse methods across multiple classes.
3. Interface vs abstract class, tell me the difference
What is an Interface?
An interface is like a contract that a class must follow. It only defines method names but not their implementation . Any class that implements an interface must provide its own implementation of the methods.
🔹 Think of an interface as a rulebook that a class must follow.
Example of an Interface in PHP
interface Animal {
public function makeSound(); // No implementation, just a method signature
}
class Dog implements Animal {
public function makeSound() {
return "Bark!";
}
}
class Cat implements Animal {
public function makeSound() {
return "Meow!";
}
}
$dog = new Dog();
echo $dog->makeSound(); // Output: Bark!
$cat = new Cat();
echo $cat->makeSound(); // Output: Meow!
What is an Abstract Class?
An abstract class is a partially built class . It can have both:
- Abstract methods (like an interface, without implementation)
- Concrete methods (with full implementation)
🔹 Think of an abstract class as a template that other classes can build upon.
Example of an Abstract Class in PHP
abstract class Vehicle {
abstract public function move(); // Must be implemented in child class
public function start() {
return "Engine started!";
}
}
class Car extends Vehicle {
public function move() {
return "The car is moving!";
}
}
$myCar = new Car();
echo $myCar->start(); // Output: Engine started!
echo $myCar->move(); // Output: The car is moving!
Key Differences: Interface vs. Abstract Class
Feature | Interface | Abstract Class |
---|---|---|
Method Implementation | No (only method names) | Yes (can have both abstract and concrete methods) |
Multiple Inheritance? | Yes (A class can implement multiple interfaces) | No (A class can extend only one abstract class) |
Default Method Behavior? | No (must be implemented in child class) | Yes (can have default method implementation) |
Summary:
✔ Use an interface when you want to enforce a structure across multiple classes.
✔ Use an abstract class when you want to provide some default behavior but also enforce method implementation in child classes.
Would you like further examples or modifications? 🚀
4. What the final keyword does?
If you prefix a function in a class with the final keyword, the child class cannot overwrite that function.
class BaseClass {
final public function moreTesting() {
echo "BaseClass::moreTesting() called\n";
}
}
class ChildClass extends BaseClass {
public function moreTesting() {
echo "ChildClass::moreTesting() called\n";
}
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
// You cannot override final methods even if they are defined as private.
5. What does the following code?
function foo(&$var)
{
$var++;
}
$a = 5;
$b = $a;
$c = $a + 7;
foo($a);
echo $a;
echo $b;
echo $c;
echo $a;
6
The function foo(&$var) takes a reference to $var, meaning any changes to $var inside the function also modify the original variable.
foo(&$a); increments $a by 1, so $a becomes 6. Removing `&` will pass a by value instead of reference
echo $b;
5
echo $c;
12
6.What are namespaces and how to use them?
Namespaces in PHP prevent naming conflicts by encapsulating entities like classes, interfaces, functions, and constants. They’re like directories for file systems, ensuring that two classes with the same name can coexist if they’re in different namespaces.
To declare a namespace, you use the namespace keyword at the top of your PHP file, before any non-comment code. Here’s a brief example:
namespace MyProject\Database;
class Connection {}
// Using the class from another file
$connection = new \MyProject\Database\Connection();
7. Explain what are closures?
A closure in PHP is an anonymous function that can capture variables from its surrounding scope. This means you can create a function without a name and pass it around as if it were a regular variable. Closures are useful for creating callback functions.
The use keyword is specifically related to closures in PHP. It allows you to inherit variables from the parent scope (where the closure is defined) into the closure itself. It’s important because, by default, the inner function (closure) does not have access to the outer scope’s variables.
$message = 'Hello';
$exampleClosure = function() use ($message) {
echo $message;
};
$exampleClosure(); // Outputs 'Hello'
8. How to handle Error in PHP
PHP provides different ways to handle errors, from simple warnings to fatal crashes. Understanding error types and handling mechanisms can help build more stable and secure applications .
Types of Errors in PHP
✅ 1. Notices
➡ Minor issues like accessing undefined variables .
➡ They don’t stop script execution .
🔹 Example:
echo $undefinedVar; // Notice: Undefined variable
⚠️ 2. Warnings
➡ More significant issues, like including a missing file .
➡ The script continues to run , but there might be unexpected behavior.
🔹 Example:
include("nonexistentfile.php"); // Warning: include(): Failed opening file
echo "This line still executes.";
❌ 3. Fatal Errors
➡ Critical issues like calling undefined functions or classes .
➡ The script stops immediately .
🔹 Example:
undefinedFunction(); // Fatal error: Uncaught Error: Call to undefined function
echo "This line will not execute.";
Error Reporting in PHP
PHP provides the error_reporting()
function to control which types of errors are displayed .
📌 Development Mode (Show All Errors)
Useful during development to debug issues:
error_reporting(E_ALL);
ini_set('display_errors', 1);
🚀 Production Mode (Hide Errors)
For security reasons, hide errors from users and log them instead:
error_reporting(0); // Suppresses error messages
ini_set('log_errors', 1);
ini_set('error_log', 'errors.log'); // Logs errors to a file
Handling Errors with set_error_handler()
PHP allows defining a custom error handler using set_error_handler()
.
Custom Error Handler Example
function customErrorHandler($errno, $errstr, $errfile, $errline) {
echo "⚠ Error [$errno]: $errstr in $errfile on line $errline \n";
return true; // Prevents PHP from executing its default error handler
}
// Set custom error handler
set_error_handler("customErrorHandler");
// Trigger an error
echo $undefinedVar; // Calls customErrorHandler()
Exception Handling with try-catch
For handling runtime errors gracefully , PHP uses try-catch
blocks.
Example: Handling Division by Zero
function divide($a, $b) {
if ($b == 0) {
throw new Exception("Division by zero is not allowed.");
}
return $a / $b;
}
try {
echo divide(10, 0);
} catch (Exception $e) {
echo "Caught exception: " . $e->getMessage();
}
Key Takeaways
✅ Notices, Warnings, and Fatal Errors define severity levels.
✅ Use error_reporting()
to control which errors are shown.
✅ Custom error handlers (set_error_handler()
) allow better control.
✅ Use try-catch
for exceptions to manage runtime errors properly.
9. Explain SOLID with examples
The SOLID principles are five design principles that help developers write maintainable and scalable object-oriented code.
1. Single Responsibility Principle (SRP)
Each class should have only one reason to change.
class Report {
public function generate() {
return "Report data";
}
}
class ReportPrinter {
public function print(Report $report) {
echo $report->generate();
}
}
Why? The Report
class only handles report generation, while ReportPrinter
handles output.
———
2. Open/Closed Principle (OCP)
A class should be open for extension but closed for modification.
interface PaymentMethod {
public function pay($amount);
}
class PayPal implements PaymentMethod {
public function pay($amount) {
echo "Paying $amount via PayPal";
}
}
class CreditCard implements PaymentMethod {
public function pay($amount) {
echo "Paying $amount via Credit Card";
}
}
Why? We can add new payment methods without modifying existing code.
———
3. Liskov Substitution Principle (LSP)
Subtypes must be substitutable for their base types.
class Bird {
public function fly() {
echo "I can fly!";
}
}
class Sparrow extends Bird {
// Sparrow flies, so no need to override fly()
}
class Penguin extends Bird {
public function fly() {
throw new Exception("I can't fly!");
}
}
function makeBirdFly(Bird $bird) {
$bird->fly();
}
$bird1 = new Sparrow();
$bird2 = new Penguin();
makeBirdFly($bird1); // Works fine
makeBirdFly($bird2); // Error: breaks LSP
Problem: Penguin
violates LSP because it changes expected behavior.
———4. Interface Segregation Principle (ISP)
No class should be forced to implement methods it does not use.
interface Printer {
public function printDocument();
}
interface Scanner {
public function scanDocument();
}
class SimplePrinter implements Printer {
public function printDocument() {
echo "Printing document";
}
}
Why? The SimplePrinter
class does not need to implement scanDocument
.
———
5. Dependency Inversion Principle (DIP)
High-level modules should not depend on low-level modules.
interface Notification {
public function send($message);
}
class EmailNotification implements Notification {
public function send($message) {
echo "Sending Email: $message";
}
}
class UserService {
private $notification;
public function __construct(Notification $notification) {
$this->notification = $notification;
}
public function notifyUser() {
$this->notification->send("Hello User!");
}
}
Why? UserService
depends on an abstraction (Notification
) rather than a concrete class.
———
Following SOLID principles makes PHP applications scalable, maintainable, and easier to test.
10. Explain Dependency Injection?
Dependency Injection (DI) is a design pattern that promotes loose coupling by injecting dependencies (objects) into a class rather than having the class create them itself. This makes the code more maintainable, testable, and flexible.
Example Without Dependency Injection
Here, the UserService
class creates a DatabaseConnection
instance internally, making it tightly coupled.
class DatabaseConnection {
public function connect() {
return "Connected to the database";
}
}
class UserService {
private $db;
public function __construct() {
$this->db = new DatabaseConnection(); // Hardcoded dependency
}
public function getUser() {
return $this->db->connect();
}
}
$userService = new UserService();
echo $userService->getUser();
Example With Dependency Injection
Now, the DatabaseConnection
is passed into UserService
, making it more flexible.
class DatabaseConnection {
public function connect() {
return "Connected to the database";
}
}
class UserService {
private $db;
public function __construct(DatabaseConnection $db) {
$this->db = $db; // Injecting the dependency
}
public function getUser() {
return $this->db->connect();
}
}
// Dependency Injection in action
$dbConnection = new DatabaseConnection();
$userService = new UserService($dbConnection);
echo $userService->getUser();
Benefits of Dependency Injection
- Loose Coupling : The class doesn’t need to create its dependencies.
- Easier Testing : Mock dependencies can be injected during unit testing.
- Improved Maintainability : Dependencies can be swapped out with minimal code changes.
- Better Scalability : Encourages modular and reusable components.
11. Composition vs Inheritance
Inheritance (“is-a” Relationship)
Inheritance allows a class to derive from another class, inheriting its properties and methods. However, it creates a tight coupling between parent and child classes, which can limit flexibility.
Example:
class Animal {
public function makeSound() {
return "Some sound";
}
}
class Dog extends Animal {
public function makeSound() {
return "Bark";
}
}
$dog = new Dog();
echo $dog->makeSound(); // Output: Bark
Composition (“has-a” Relationship)
Composition involves creating objects with references to other objects instead of extending classes. This promotes flexibility and code reuse.
Example:
class Engine {
public function start() {
return "Engine started";
}
}
class Car {
private $engine;
public function __construct(Engine $engine) {
$this->engine = $engine;
}
public function startCar() {
return $this->engine->start();
}
}
$engine = new Engine();
$car = new Car($engine);
echo $car->startCar(); // Output: Engine started
When to Use Composition Over Inheritance?
- More Flexibility : Composition allows dynamic behavior changes without modifying the base class.
- Avoiding Deep Hierarchies : Deep inheritance chains can make maintenance difficult.
- Reusability : Components can be reused across multiple classes without extending them.
- Better Testing : Individual components can be tested separately.
In most cases, composition is preferred over inheritance because it provides better modularity and maintainability. 🚀
1. What does PHP stand for and what is its main purpose?
PHP originally represented “Personal Home Page,” signifying its early focus on web development. It has since evolved to suggest “PHP: Hypertext Preprocessor,” emphasizing its role in server-side scripting and building dynamic web content.
Core Functions
- Generating Dynamic Content: PHP is adept at generating dynamic web content, web pages, images, and more.
- Handling Form Data: It efficiently processes form data from HTML input fields.
- Accessing Databases: PHP can interact with databases, empowering dynamic content storage and management.
- Session Management: It enables web state management, crucial for maintaining user context across multiple requests.
- File System Interaction: PHP can manipulate files on the server filesystem.
- Email Sending: It provides the capability to send emails directly from the server.
- User Authentication: PHP can authenticate users and manage their access within web applications.
What PHP Is and Isn’t
- Server-Side Scripting Language: PHP excels in orchestrating server operations, including complex storage and retrieval tasks.
- HTML Embedding Compatibility: Its syntax within web documents is reminiscent of HTML, interleaving with the content for seamless integration.
- Not Purely Object-Oriented: While it now supports object-oriented programming paradigms, it continues to offer primarily procedural constructs.
- Text Pre-Processor and Interpreter: PHP initially parses embedded code within text via the pre-processor, swiftly executing it to yield HTML or other output.
2. How do you execute a PHP script from the command line?
Executing a PHP script from the command line involves using the php
CLI tool.
Using the php
Command
To run a PHP script, use the following command:
php your_script.php
Arguments
- Input: The
-f
option allows you to provide a file. - Output: Use
-i
to see the configuration, and-r
to run a snippet without a script. - When Installed:
--run
is an alternative for Unix systems without a shebang line. - PHP Version: Use
-v
to check the installed PHP version.
Examples
-
Running a File:
php -f script.php
-
Displaying PHP Info:
php -i
-
Running a Single Command:
php -r 'echo "Hello, PHP!";'
Setting Up Environment Variables
You can configure PHP-specific environment variables, allowing for script customization or convenience. For example:
- Using a different configuration file:
php -c <custom-config>.ini -f script.php
- Customizing extensions’ path:
PHP_INI_SCAN_DIR=/path/to/extensions php -f script.php
Managing the Standard Input/Output Channels
By default, PHP’s CLI environment allows input from the terminal or using pipes. It prints output to the terminal.
Redirections and Pipelines, such as >
or |
, can be leveraged for customizing how input and output are handled.
Redirections
- Sending output to a file:
php script.php > output.txt
- Appending to a file:
php script.php >> output.txt
- Reading from a file:
php script.php < input.txt
Pipelines
Pipelines can be used for more complex I/O operations. The following example involves running script.php
, which produces a list of URLs, and then the crawler.php
script visits each of those URLs:
php script.php | php crawler.php
Running PHP from Non-Unix Systems
On certain platforms, you might need to use php-cgi
or specify the .exe
extension. For instance:
- Windows:
php-cgi.exe your_script.php
- macOS:
/usr/bin/php your_script.php
It’s also common to need to add PHP to your system’s path or reference PHP from an absolute path.
<br>
3. Can you describe the differences between PHP 5 and PHP 7/8?
Migrating from PHP 5 to PHP 7/8 provides significant improvements in performance, security, and features. However, this transition involves several changes that need to be navigated.
Key Improvements in PHP 7/8
01. Scalar Type Declarations
- PHP 5: Lacked strict scalar typing.
- PHP 7/8: Supports both
declare(strict_types=1);
for individual files and scalar type hints (int, float, bool, string) in function/method signatures.
02. Return Type Declarations
- PHP 5: Couldn’t specify return types.
- PHP 7/8: Enables declaring specific return types using inline notations.
03. Null Coalescing Operator:
- PHP 5: Absent.
- PHP 7/8: Introduced the
??
operator, streamlining null checks.
04. Spaceship Operator
- PHP 5: Lacked support.
- PHP 7/8: Introduced the
<=>
operator for clearer comparisons.
05. Constant Array/Object Definitions
- PHP 5: Limited to defined constants.
- PHP 7/8: Allows defining arrays and objects with the
define
keyword.
06. Anonymous Classes
- PHP 5: Lacked support for on-the-fly class definition.
- PHP 7/8: Introduced classes without explicit declarations.
07. Iterable Type Hint
- PHP 5: No specific hint for iterable types.
- PHP 7/8: Offers the
iterable
type hint, providing a generic type for traversable data structures.
08. CSPRNG Functions
- PHP 5: Weaker random number generation.
- PHP 7/8: Provides stronger cryptographic random number functions like
random_bytes
andrandom_int
.
09. Anonymous Functions
- PHP 5: Required the
use
keyword for accessing outer scope. - PHP 7/8: They are now able to automatically capture variables from the outer scope, which eases the syntax.
Changes in PHP 7 and 8
Nullable Return Types
- PHP 7.1: Introduced the
?Type
notation to indicate that a function can return either the specified type ornull
.
Type Declaration Tweaks in PHP 7.4 and PHP 8
- PHP 7.4: The
typed_properties=1
directive for strict typing at the class level. - PHP 8: Introduced
::class
constant that returns the class name.
Union Types
- PHP 8: Ability to specify union types in method/function signatures, defining multiple possible return types separated by vertical bars. Example:
function foo(): int|bool
. - Initial PHP 7.1 Support: The
iterable
type hint was introduced in PHP 7.1.
Match Expressions
- PHP 8: Offers the
match
/case
expression as a more precise and powerful variant ofswitch
statements.
Named Arguments
- PHP 8: Allows passing arguments to functions based on their parameter names rather than positions, enhancing clarity.
<br>
4. What are the common ways to embed PHP into HTML?
While there are several ways to embed PHP within HTML, the <?php
tag, which encloses PHP code, is the most widely used. It’s important to note that the choice of method should align with the practical needs of your project.
Common Methods of Embedding PHP in HTML
PHP Short Tags (<? ... ?>
)
- Advantages: More concise and readable.
- Drawbacks: Not always enabled; deprecated after PHP v7.0.
ASP-Style Tags (<% ... %>
, <%= ... %>
, <%# ... %>
)
- Advantages: Familiar to ASP developers.
- Drawbacks: Not default behavior; must be enabled.
Script Tags (<script language="php"> ... </script>
)
- Advantages: Can be useful in very specific cases.
- Drawbacks: phpBB and Bugs.
Apache Server Embedding (< ? ... ?>
)
- Advantages: No need for PHP module.
- Drawbacks: Integration concerns.
Basic PHP Tag (<?php ... ?>
)
These tags are always a safe choice and offer the highest compatibility across platforms.
Syntax
<?php
// Your PHP code here
?>
It’s worth noting that <?=
is a shortcut equivalent to <?php echo
, available in all versions beyond PHP v5.4.
Practical Use-Cases & Benefits
- Standardized, cross-platform approach.
- Compatible with all PHP builds and hosting environments.
- Enhanced readability and maintainability.
Code Playground
Here is the PHP code:
<!DOCTYPE html>
<html>
<head>
<title>PHP in HTML</title>
</head>
<body>
<?php
$name = "John";
echo "<h1>Welcome, $name!</h1>";
?>
</body>
</html>
5. How would you create a PHP variable and how are they scoped (global, local, static)?
PHP variables have diverse scopes, from being accessible globally by all scripts to being confined to defined functions or methods. They can be local, global, and static.
Local Scope
Variables defined within a function are locally scoped and inaccessible outside its body.
Example: Local Scope
Here is the PHP code:
function myFunc() {
$localVar = "I am local";
echo $localVar; // Outputs: I am local
}
myFunc();
echo $localVar; // Throws an error
Global Scope
Global variables can be accessed across the entire PHP script, including from within functions.
Example: Global Scope
Here is the PHP code:
$globalVar = "I am global";
function myFunc() {
echo $globalVar; // Outputs: I am global
}
myFunc();
echo $globalVar; // Outputs: I am global
Function / Method Scope
Variables declared within a function or method are limited in scope to that block.
Example: Function Scope
Here is the PHP code:
function myFunc() {
$functionVar = "I am function-scoped";
echo $functionVar; // Outputs: I am function-scoped
}
myFunc();
echo $functionVar; // Throws an error
Static Scope
Static variables retain their values between function calls. They are still function-scoped.
Example: Static Scope
Here is the PHP code:
function counter() {
static $count = 0;
$count++;
echo $count;
}
counter(); // Outputs: 1
counter(); // Outputs: 2
counter(); // Outputs: 3
Superglobals
In PHP, some special predefined arrays, such as $_POST
and $_GET
, are super global and have a global scope. They are accessible from any part of the code, including within functions and methods.
<br>
6. Explain the data types that are supported in PHP.
PHP supports various data types, each serving a distinct role.
Core Data Types
-
Integer (
int
in PHP 7,integer
in earlier versions): Represents whole numbers, both positive and negative.- Example:
$age = 30;
- Example:
-
Floating-Point Number (
float
): Represents decimal numbers, also known as floats or doubles.- Example:
$price = 9.99;
- Example:
-
String (
string
): Signifies sequences of characters, enclosed within single or double quotes.- Example:
$name = "John";
- Example:
-
Boolean (
bool
): Represents logical states -true
orfalse
.- Example:
$isStudent = true;
- Example:
- Resource: Placeholder for external resources, such as database connections.
- Null: Denotes the absence of a value.
Compound Data Types
- Array: A flexible and indexed data structure that can hold multiple values of different data types.
- Object: Instances of defined classes that encapsulate data and behavior.
- Callable: Ensures that a variable is a valid function or method.
-
Iterable: Introduced in PHP 7.1. Any data type that can be looped via
foreach
.- Example:
array
andTraversable
(interface implemented by arrays and classes that are loop-able).
- Example:
Special Types
PHP has two special types:
- Pseudotype: These are not actual data types but are considered basic types in PHP.
- Literal: Introduced in PHP 8, such as
mixed
, that can accept multiple primitive types.
Code Example: Complex Data Types
Here is the PHP code:
// Create associative array
$person = [
'name' => 'Alice',
'age' => 25,
'isStudent' => true
];
// Define class
class Car {
public $make;
public $model;
public function __construct($make, $model) {
$this->make = $make;
$this->model = $model;
}
}
// Instantiate Car object
$myCar = new Car('Toyota', 'Corolla');
// Define function that takes callable parameter
function testFunction(callable $callback) {
$callback();
}
// Call function and pass an anonymous function
testFunction(function() {
echo "Callback executed!";
});
7. How does PHP handle error reporting?
In PHP, Error Handling can be configured using either .ini
settings, programmatic functions, or a combination of both, offering developers great flexibility.
Configuration Modes
- Local (File-Specific): Adjusts settings for a specific PHP file using
ini_set()
. - Global: Modifies global PHP settings via
php.ini
orini_set()
.
Enabling Error Reporting
- Using Functions:
error_reporting(E_ALL)
enables all types of errors. To target specific error types, bitwise operators come in handy. - Using php.ini: Directly edit the
php.ini
file. Settingerror_reporting
toE_ALL
enables comprehensive reporting. - Using ini_set(): For finer control, use
ini_set('error_reporting', E_ALL)
when you need to adjust settings on a per-file basis.
Or direct the errors to a display or a log:
- To display errors on the screen, configure
display_errors
asOn
. - To log errors to a file, enable them by setting
log_errors
toOn
and define the log file witherror_log
.
Error Types
- E_NOTICE: Informs about non-critical discrepancies.
- E_WARNING: Alerts about more critical problems.
- E_ERROR: Indicates serious faults that halt script execution.
- E_PARSE: Arises from parse errors, such as syntax mistakes.
- E_STRICT: Suggests updates to code for better interoperability.
- E_DEPRECATED: Flags features that are outdated and might be removed in future versions.
- E_RECOVERABLE_ERROR: Major issues that still allow script execution.
Combining Flags
Developers can use error_reporting()
in conjunction with bitwise operators to set multiple flags. For example:
error_reporting(E_ALL & ~E_DEPRECATED)
reports all errors except deprecation notices.error_reporting(E_ERROR | E_WARNING | E_PARSE)
reports only errors, warnings, and parse errors.
Code Validator
Here is the PHP code:
// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Generate a warning
$totalCost = 100;
$availableFunds = 50;
if ($totalCost > $availableFunds) {
trigger_error("Insufficient funds!", E_USER_WARNING);
}
// Generate a fatal error
require 'non_existent_file.php';
// Will not reach this point due to the fatal error above
echo "This will never be displayed.";
8. What is the purpose of php.ini file?
The php.ini file is the configuration center for PHP settings, governing a range of operational aspects. It is an essential tool for managing a server’s PHP environment.
Key Functions
- Settings Management: The file allows for the configuration of PHP settings, offering granular control over key directives such as memory_limit and error_reporting.
- Environment Tailoring: By modifying php.ini, developers can fine-tune PHP to best suit their specific applications and environments.
- Error and Security Settings: The file provides a centralized location to manage error reporting, display, and log settings, alongside various security-related configurations.
PHP Versions and Editions
- It’s important to note that \foo` variable.
- The file can have different variations across PHP versions, and its absence can pose a problem when troubleshooting.
PHP Modes
- Per-Directory Basis: Some servers permit PHP settings to be defined locally within directories via .htaccess or lighttpd.conf files.
- Run-Time Editing: Certain settings can be reconfigured dynamically via ini_set during script execution.
Recommendations
- Runtime Security: Encrypt or protect the php.ini file to prevent unauthorized access, particularly in environments involving shared hosting.
- Centralized Management: Utilize Version Control Systems (VCS) or configuration management tools to maintain and track changes in the php.ini file.
- Regular Audits: Review the php.ini file periodically to ensure it aligns with security best practices and application requirements.
<br>
9. How do you define a constant in PHP?
In PHP, a constant is a named identifier whose value remains consistent during the execution of a script.
Key define()
features
- Case-Sensitivity: Constants are not case-sensitive by default.
- Global Scope: Constants can be accessed from any part of the code without additional requirements.
- Value Types: Constants can hold values like integers, floats, strings, or arrays.
Syntax: define(NAME, value, case-insensitive)
- NAME: The designated constant name (specific naming rules apply).
- value: The constant’s assigned literal value or expression.
- case-insensitive (Optional): A boolean flag (
true
for case-insensitive) determining if the constant’s name is case-sensitive.
Code Example: Defining Constants
Here is the PHP code:
// Case-sensitive constant
define("GREETING", "Hello, World!");
// Case-insensitive constant
define("SITE_NAME", "MySite", true);
// Accessing constants
echo GREETING; // Output: "Hello, World!"
echo SITE_NAME; // Output: "MySite" or "MYSITE"
Best Practice
- Unique Names: Use distinct, self-explanatory names to avoid unintended overwrites or misinterpretations.
- Error Reporting: Pay attention to constant re-declarations or undefined constants to ensure script reliability.
- Initialization: Ideally, constants should be defined within the script’s beginning to ensure consistent values across the application.
- Code Clarity: Employ uppercase letters and underscores to boost constant visibility and readability.
- Constants Beyond Strings: While strings are frequently used, note that constants can store various data types like integers, floats, and arrays.
<br>
10. Can you describe the lifecycle of a PHP request?
Understanding the detailed lifecycle of a PHP request will help you optimize your web applications for better performance.
Stages of a PHP Request
-
Bootstrap
- Code in your
index.php
file initializes the PHP environment.
- Code in your
-
Pre-Processing
- PHP compiles the requested file into opcode, if necessary.
- The Zend Engine, which powers PHP, loads necessary extensions and sets up internal structures.
-
Request Processing
- PHP scripts execute from top to bottom, unless there’s a redirect, error, or exit.
-
Output Buffering
- The
ob_
family of functions handles application output buffering.
- The
-
Response
- When execution completes, the built-up output is sent back to the webserver for final delivery to the client.
The Engine Behind the Scenes
- httpd: Apache and Nginx are popular HTTP servers that manage incoming requests.
- PHP Parser: Translates human-readable PHP code into machine-readable instructions.
Web Server Handover
- When a web server, such as Apache or Nginx, processes an incoming HTTP request, it detects PHP as the handler for
.php
files and launches the PHP parser.
Halted Behavior
One of the stumbling blocks for new PHP developers to get to grips with is that setting local redirects will halt script execution:
header('Location: /new_page.php');
exit;
One notable example of this behavior, especially in one-page (or one-script) applications, is the usage of the exit
construct right after setting a location header. This abrupt exit can sometimes become problematic in larger projects or if not carefully managed. It is often more advisable to architect your applications with a more streamlined version of redirects and exits; consider using the “inverted if” approach to reduce nested levels.
<br>
11. Explain the use of sessions in PHP.
Sessions enable secure storage and retrieval of user information throughout their interaction with a web application.
Key Components
- Session Creation: Starts when a user accesses a web page and initializes a session, providing a unique session ID for that user.
- Data Persistence: Allows data to persist across different pages, often using session cookies.
- Data Lifetime: Information remains accessible during the user’s visit and can be configured to extend over multiple visits.
Implementing Sessions in PHP
Starting a session in PHP is straightforward, and many frameworks handle this process automatically. Simply call session_start()
at the beginning of each PHP script.
// Initialize session
session_start();
You can then use super-global variable $_SESSION
to store and retrieve data.
Methods of Starting a Session
- Automatic: Set
session.auto_start
to 1 inphp.ini
, and the session begins for all pages. - Manual: Starts when a PHP script calls
session_start()
explicitly.
Configuring Session Parameters
You can control session behavior and security using session_start()
and session_set_cookie_params()
. Here’s the breakdown:
- Session timeout: Set the session lifetime using
session.gc_maxlifetime
. Sessions might be deleted by the PHP garbage collector if not accessed within this time. - Cookie parameters: Configure session cookies for secure, HTTP-only, and domain-specific behavior.
- Token-based protection: Use CSRF tokens to safeguard against Cross-Site Request Forgery.
Security Measures
Sessions are highly valuable but require vigilance for security. Here are some best practices:
- SSL/TLS Encryption: Secure the entire session with a proper SSL/TLS certificate.
- Session Fixation Prevention: Generate a fresh session ID upon user authentication to deter session fixation attacks.
- Session Hijacking Prevention: Regularly switch session IDs and restrict sessions to the user’s IP address or user agent if feasible.
<br>
12. How does PHP support cookies?
Cookies are HTTP headers that help websites remember users. In PHP, you can achieve seamless cookie management using built-in functions.
PHP Functions for Cookie Handling
- setcookie: Creates a new cookie or modifies an existing one.
- $_COOKIE: A global associative array that holds all set cookies, accessible from any script.
- $_COOKIE[ ‘cookieName’ ]: Particularly useful for reading cookie values.
- Example of Setcookie: Take a look!
// Set cookie with a value that expires in 24 hours
setcookie('username', 'JohnDoe', time()+86400, '/', '.example.com', true);
Common Cookie Parameters
- Name: The cookie’s unique identifier.
- Value: Data associated with the cookie.
- Expiration: Time when the cookie should expire.
- Path: The directory for which the cookie is valid.
- Domain: The domain for which the cookie is valid.
- Secure: Specifies if the cookie should be sent only over secure (HTTPS) connections.
- HttpOnly: When set to
true
, the cookie is accessible only through HTTP protocols.<br>
Below is a comprehensive list of 100 PHP Interview Questions for middle to senior level candidates. Each question is paired with an explanation and a code snippet (when applicable) to help illustrate the concept.
1. Explain Namespaces in PHP
Question: What are namespaces and how do they help avoid class name collisions?
Explanation: Namespaces allow you to group related classes, functions, and constants under a unique name. They help prevent naming conflicts when using multiple libraries.
Code Example:
<?php
namespace MyApp\Utils;
class Helper {
public static function greet($name) {
return "Hello, $name!";
}
}
// Usage from the global namespace:
echo \MyApp\Utils\Helper::greet("World");
?>
2. What Are Traits and How Do You Use Them?
Question: Describe traits and demonstrate how to reuse methods across classes.
Explanation: Traits enable you to reuse sets of methods in multiple classes, overcoming PHP’s single inheritance limitation.
Code Example:
<?php
trait Logger {
public function log($msg) {
echo "[LOG]: " . $msg;
}
}
class User {
use Logger;
}
$user = new User();
$user->log("User logged in.");
?>
3. Explain Dependency Injection in PHP
Question: How does dependency injection improve code testability and maintainability?
Explanation: By injecting dependencies (like services or components) rather than hardcoding them, your code becomes easier to test, maintain, and reuse.
Code Example:
<?php
interface MailerInterface {
public function send($recipient, $message);
}
class SmtpMailer implements MailerInterface {
public function send($recipient, $message) {
// send email via SMTP...
echo "Sending via SMTP to $recipient: $message";
}
}
class Notification {
protected $mailer;
public function __construct(MailerInterface $mailer) {
$this->mailer = $mailer;
}
public function notify($user, $msg) {
$this->mailer->send($user, $msg);
}
}
$mailer = new SmtpMailer();
$notification = new Notification($mailer);
$notification->notify("user@example.com", "Hello!");
?>
4. What Are Closures and How Can They Be Used?
Question: Explain anonymous functions (closures) and provide a scenario for their use.
Explanation: Closures are functions without names that can capture variables from the surrounding scope, making them ideal for callbacks.
Code Example:
<?php
$numbers = [1, 2, 3, 4, 5];
$squared = array_map(function($n) {
return $n * $n;
}, $numbers);
print_r($squared);
?>
5. Implementing the Singleton Pattern
Question: How do you implement the Singleton pattern in PHP?
Explanation: The Singleton pattern ensures a class has only one instance and provides a global point of access.
Code Example:
<?php
class Singleton {
private static $instance = null;
private function __construct() { }
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
}
// Usage:
$instance1 = Singleton::getInstance();
$instance2 = Singleton::getInstance();
var_dump($instance1 === $instance2); // true
?>
6. Implementing the Abstract Factory Pattern
Question: How can the abstract factory pattern be implemented in PHP for creating families of related objects?
Explanation: The abstract factory provides an interface for creating related objects without specifying their concrete classes.
Code Example:
<?php
interface Button {
public function render();
}
class WinButton implements Button {
public function render() {
return "Windows Button";
}
}
class MacButton implements Button {
public function render() {
return "Mac Button";
}
}
interface GUIFactory {
public function createButton(): Button;
}
class WinFactory implements GUIFactory {
public function createButton(): Button {
return new WinButton();
}
}
class MacFactory implements GUIFactory {
public function createButton(): Button {
return new MacButton();
}
}
// Usage:
function renderButton(GUIFactory $factory) {
$button = $factory->createButton();
echo $button->render();
}
renderButton(new WinFactory()); // Outputs "Windows Button"
?>
7. Using Interfaces in PHP
Question: What is the purpose of interfaces and how do you implement them?
Explanation: Interfaces define a contract for classes without providing implementation details.
Code Example:
<?php
interface PaymentGateway {
public function charge($amount);
}
class StripeGateway implements PaymentGateway {
public function charge($amount) {
return "Charging $$amount via Stripe.";
}
}
$gateway = new StripeGateway();
echo $gateway->charge(100);
?>
8. The __call() Magic Method
Question: How does the call() magic method work and when would you use it?
**Explanation: call() is triggered when invoking inaccessible or non-existent methods, allowing dynamic method handling.
**Code Example:
<?php
class DynamicMethods {
public function __call($name, $arguments) {
return "Called method '$name' with arguments: " . implode(', ', $arguments);
}
}
$obj = new DynamicMethods();
echo $obj->undefinedMethod("arg1", "arg2");
?>
9. Autoloading with spl_autoload_register
Question: Explain how autoloading works in PHP using spl_autoload_register
.
Explanation: Autoloading automatically loads classes when they’re instantiated, eliminating the need for manual includes.
Code Example:
<?php
spl_autoload_register(function ($class) {
include_once str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
});
// Assuming file structure: MyApp/Service/MyService.php
// And class declared as: namespace MyApp\Service; class MyService { ... }
$service = new \MyApp\Service\MyService();
?>
10. PSR-4 Autoloading Standard
Question: What is PSR-4 autoloading and how is it configured in Composer?
Explanation: PSR-4 defines a standard for autoloading classes from file paths that correspond to their namespaces.
Code Example (composer.json snippet):
{
"autoload": {
"psr-4": {
"MyApp\\": "src/"
}
}
}
After updating composer, classes under src/
will be autoloaded according to their namespace.
11. Static vs. Non-Static Methods
Question: What is the difference between static and non-static methods in PHP?
Explanation: Static methods belong to the class and can be called without an instance, while non-static methods require an object.
Code Example:
<?php
class Example {
public static function staticMethod() {
return "I'm static!";
}
public function instanceMethod() {
return "I'm an instance method!";
}
}
// Usage:
echo Example::staticMethod();
$instance = new Example();
echo $instance->instanceMethod();
?>
12. Late Static Binding
Question: Explain late static binding and demonstrate its use.
Explanation: Late static binding allows a method in a base class to call overridden methods in child classes.
Code Example:
<?php
class Base {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who(); // late static binding
}
}
class Child extends Base {
public static function who() {
echo __CLASS__;
}
}
Child::test(); // outputs "Child"
?>
13. Using Reflection in PHP
Question: What is PHP’s Reflection API used for?
Explanation: Reflection allows inspection of classes, methods, and properties at runtime, enabling dynamic code analysis.
Code Example:
<?php
class Sample {
private $data = "secret";
public function getData() {
return $this->data;
}
}
$reflection = new ReflectionClass('Sample');
$properties = $reflection->getProperties();
foreach ($properties as $prop) {
echo $prop->getName() . "\n";
}
?>
14. Error Handling with Try-Catch
Question: How do you implement error handling in PHP using try-catch?
Explanation: Try-catch blocks allow you to handle exceptions and errors gracefully, preventing application crashes.
Code Example:
<?php
try {
if (!file_exists("somefile.txt")) {
throw new Exception("File not found.");
}
$data = file_get_contents("somefile.txt");
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
?>
15. PHP Type Declarations
Question: How do you use scalar type declarations and return types in PHP?
Explanation: PHP allows you to enforce function parameter types and return types for improved code reliability.
Code Example:
<?php
function addNumbers(int $a, int $b): int {
return $a + $b;
}
echo addNumbers(5, 10); // 15
?>
16. Anonymous Classes
Question: How do you create and use an anonymous class in PHP?
Explanation: Anonymous classes allow quick, one-off class definitions without naming them explicitly.
Code Example:
<?php
$object = new class {
public function sayHello() {
return "Hello from anonymous class!";
}
};
echo $object->sayHello();
?>
17. Using Generators in PHP
Question: What are generators and how do they improve memory usage?
Explanation: Generators yield values one at a time, allowing you to iterate over large data sets without loading everything into memory.
Code Example:
<?php
function counter($start, $end) {
for ($i = $start; $i <= $end; $i++) {
yield $i;
}
}
foreach (counter(1, 5) as $number) {
echo $number . " ";
}
?>
18. Implementing Iterators
Question: How do you implement the Iterator interface for custom iteration?
Explanation: Implementing Iterator allows an object to be iterated using foreach by defining how to traverse its data.
Code Example:
<?php
class MyIterator implements Iterator {
private $data = [];
private $index = 0;
public function __construct(array $data) {
$this->data = array_values($data);
}
public function current() {
return $this->data[$this->index];
}
public function key() {
return $this->index;
}
public function next() {
++$this->index;
}
public function rewind() {
$this->index = 0;
}
public function valid() {
return isset($this->data[$this->index]);
}
}
$iterator = new MyIterator(['a', 'b', 'c']);
foreach ($iterator as $key => $value) {
echo "$key => $value\n";
}
?>
19. Composer – Dependency Management
Question: What is Composer and how do you define dependencies in composer.json
?
Explanation: Composer is PHP’s dependency manager. It allows you to declare libraries your project depends on and automatically manages (install/update) them.
Code Example (composer.json snippet):
{
"require": {
"monolog/monolog": "^2.0"
},
"autoload": {
"psr-4": {
"MyApp\\": "src/"
}
}
}
Run composer install
to download dependencies.
20. PSR Standards Overview
Question: What are PSR-1, PSR-2, and PSR-12, and why are they important?
Explanation: These PHP Standard Recommendations define coding styles and guidelines to ensure consistency and interoperability across PHP projects.
No code sample is needed.
21. Securing Against SQL Injection
Question: How do you protect your PHP application from SQL injection?
Explanation: Use prepared statements with bound parameters when interacting with databases to avoid injection attacks.
Code Example:
<?php
$pdo = new PDO("mysql:host=localhost;dbname=testdb", "user", "pass");
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = :email");
$stmt->execute(['email' => $_POST['email']]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
?>
22. Session Management in PHP
Question: How do you start a session and store data securely in PHP?
Explanation: PHP sessions allow you to store user data across multiple requests; always start sessions securely and use proper configurations.
Code Example:
<?php
session_start();
$_SESSION['user'] = [
'id' => 1,
'name' => 'John Doe'
];
echo "Session started for " . $_SESSION['user']['name'];
?>
23. Implementing Caching (APCu Example)
Question: How do you implement caching in PHP using APCu?
Explanation: APCu is an in-memory caching solution to improve performance by storing frequently accessed data.
Code Example:
<?php
$key = 'my_cache_key';
$data = apcu_fetch($key);
if ($data === false) {
// Data not found in cache; compute or fetch it.
$data = "This is cached data.";
apcu_store($key, $data, 3600); // cache for 1 hour
}
echo $data;
?>
24. Using cURL in PHP
Question: How do you make an HTTP GET request using cURL in PHP?
Explanation: cURL provides a library to communicate with other servers using various protocols.
Code Example:
<?php
$ch = curl_init("https://api.example.com/data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
25. Building a RESTful API
Question: How do you create a simple RESTful API endpoint in PHP?
Explanation: Using HTTP methods and proper headers, you can build an API endpoint that responds in JSON format.
Code Example:
<?php
header("Content-Type: application/json");
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
$data = ['message' => 'Hello, GET request received!'];
echo json_encode($data);
} else {
http_response_code(405);
echo json_encode(['error' => 'Method Not Allowed']);
}
?>
26. Secure File Uploads
Question: How do you handle file uploads securely in PHP?
Explanation: Validate file type, size, and check for errors before moving the uploaded file.
Code Example:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
$file = $_FILES['file'];
// Check file type and size
if ($file['error'] === UPLOAD_ERR_OK && $file['size'] < 2 * 1024 * 1024) {
$allowedTypes = ['image/jpeg', 'image/png'];
if (in_array($file['type'], $allowedTypes)) {
$uploadPath = __DIR__ . '/uploads/' . basename($file['name']);
move_uploaded_file($file['tmp_name'], $uploadPath);
echo "File uploaded successfully!";
} else {
echo "Invalid file type.";
}
}
}
?>
<!-- HTML Form -->
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="Upload" />
</form>
27. Factory Design Pattern
Question: Explain and implement the factory design pattern in PHP.
Explanation: The factory pattern creates objects without exposing the creation logic, using a method to decide which class to instantiate.
Code Example:
<?php
interface Car {
public function drive();
}
class Sedan implements Car {
public function drive() {
return "Driving a sedan";
}
}
class Suv implements Car {
public function drive() {
return "Driving an SUV";
}
}
class CarFactory {
public static function create($type): Car {
if ($type === 'sedan') {
return new Sedan();
} elseif ($type === 'suv') {
return new Suv();
}
throw new Exception("Car type not recognized.");
}
}
// Usage:
$car = CarFactory::create('suv');
echo $car->drive();
?>
28. Explaining the MVC Pattern
Question: What is the MVC pattern and how is it implemented in PHP?
Explanation: MVC (Model-View-Controller) separates concerns by dividing an application into three interconnected components, enhancing maintainability.
Code Structure Example:
/app
/controllers
HomeController.php
/models
User.php
/views
home.php
index.php
Inside HomeController.php
:
<?php
class HomeController {
public function index() {
$user = new User();
$data = $user->getData();
include 'views/home.php';
}
}
?>
29. Avoiding Class Name Conflicts with Namespaces
Question: How do namespaces help avoid naming conflicts?
Explanation: By encapsulating classes in different namespaces, you can have classes with the same name coexist without collision.
Code Example:
<?php
namespace Payment\Gateway;
class Stripe {
public function process() {
return "Processing Stripe payment";
}
}
namespace Notification;
class Stripe {
public function send() {
return "Sending Stripe notification";
}
}
// Usage:
$stripePayment = new \Payment\Gateway\Stripe();
echo $stripePayment->process();
$stripeNotification = new \Notification\Stripe();
echo $stripeNotification->send();
?>
30. The __invoke() Magic Method
Question: How does invoke() work and when would you use it?
**Explanation: invoke() allows an object to be used as a function, enabling a callable object interface.
**Code Example:
<?php
class CallableClass {
public function __invoke($name) {
return "Hello, $name!";
}
}
$obj = new CallableClass();
echo $obj("Alice");
?>
31. Custom Exception Handling
Question: How do you create and use custom exception classes in PHP?
Explanation: Creating custom exceptions helps in categorizing errors and handling them appropriately.
Code Example:
<?php
class CustomException extends Exception {
public function errorMessage() {
return "Custom Error: " . $this->getMessage();
}
}
try {
throw new CustomException("Something went wrong!");
} catch (CustomException $e) {
echo $e->errorMessage();
}
?>
32. Anonymous Functions
Question: What are anonymous functions and how are they used?
Explanation: Anonymous functions (closures) allow you to define functions without names, typically used as callbacks.
Code Example:
<?php
$greet = function($name) {
return "Hello, $name!";
};
echo $greet("Bob");
?>
33. Using PDO for Database Operations
Question: How do you connect to a MySQL database using PDO?
Explanation: PDO provides a uniform way to access multiple databases with prepared statements for security.
Code Example:
<?php
try {
$pdo = new PDO("mysql:host=localhost;dbname=testdb", "username", "password");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully!";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
34. Handling Transactions with PDO
Question: How do you perform transactions using PDO?
Explanation: Transactions ensure that a series of database operations are executed as a single unit, maintaining data integrity.
Code Example:
<?php
try {
$pdo->beginTransaction();
$pdo->exec("INSERT INTO accounts (name, balance) VALUES ('Alice', 1000)");
$pdo->exec("INSERT INTO accounts (name, balance) VALUES ('Bob', 1000)");
$pdo->commit();
echo "Transaction completed.";
} catch (Exception $e) {
$pdo->rollBack();
echo "Failed: " . $e->getMessage();
}
?>
35. Understanding == vs. ===
Question: What is the difference between == and === in PHP?
Explanation: The == operator compares values after type juggling, whereas === checks both value and type for equality.
Code Example:
<?php
$a = "123";
$b = 123;
if ($a == $b) {
echo "Equal using ==";
}
if ($a !== $b) {
echo "Not identical using ===";
}
?>
36. The clone Keyword
Question: How does cloning an object work in PHP?
Explanation: The clone
keyword creates a shallow copy of an object. Deep copies require additional handling.
Code Example:
<?php
class Person {
public $name;
}
$person1 = new Person();
$person1->name = "John";
$person2 = clone $person1;
$person2->name = "Jane";
echo $person1->name; // Outputs "John"
echo $person2->name; // Outputs "Jane"
?>
37. Memory Management in PHP
Question: How does PHP manage memory and what is garbage collection?
Explanation: PHP uses reference counting and a cyclic garbage collector to free memory occupied by objects that are no longer in use.
No code sample is required.
38. PHP Session Storage Mechanisms
Question: How can you configure PHP to store sessions in different storage (e.g., Redis, database)?
Explanation: PHP allows custom session handlers so you can store session data outside of files for scalability and security.
Code Example (using Redis session handler):
<?php
ini_set('session.save_handler', 'redis');
ini_set('session.save_path', 'tcp://127.0.0.1:6379');
session_start();
$_SESSION['user'] = 'Alice';
?>
39. Using microtime()
Question: What is the purpose of the microtime()
function and provide an example?
Explanation: microtime()
returns the current Unix timestamp with microseconds, useful for measuring script execution time.
Code Example:
<?php
$start = microtime(true);
// ... perform some task ...
$end = microtime(true);
echo "Execution time: " . ($end - $start) . " seconds.";
?>
40. The SPL (Standard PHP Library)
Question: How can you use SPL data structures in PHP?
Explanation: SPL provides a set of standard classes and interfaces like SplStack, SplQueue, and SplHeap to implement data structures.
Code Example (using SplStack):
<?php
$stack = new SplStack();
$stack->push("first");
$stack->push("second");
echo $stack->pop(); // Outputs "second"
?>
41. Implementing Event-Driven Programming
Question: How do you implement a simple event dispatcher in PHP?
Explanation: An event dispatcher allows you to register listeners for specific events and execute them when the event is triggered.
Code Example:
<?php
class EventDispatcher {
private $listeners = [];
public function addListener($event, callable $listener) {
$this->listeners[$event][] = $listener;
}
public function dispatch($event, $data = null) {
if (!empty($this->listeners[$event])) {
foreach ($this->listeners[$event] as $listener) {
$listener($data);
}
}
}
}
$dispatcher = new EventDispatcher();
$dispatcher->addListener('user.registered', function($user) {
echo "Welcome, " . $user;
});
$dispatcher->dispatch('user.registered', 'Alice');
?>
42. Internationalization (i18n) with gettext
Question: How do you implement internationalization in PHP using gettext?
Explanation: Gettext enables you to translate your application’s strings to support multiple languages.
Code Example:
<?php
// Set the locale and domain
putenv('LC_ALL=fr_FR');
setlocale(LC_ALL, 'fr_FR');
bindtextdomain("messages", "./locale");
textdomain("messages");
echo gettext("Hello, world!");
?>
43. Composer Autoload Class Mapping
Question: How do you configure Composer for autoloading your classes?
Explanation: Composer can generate an autoloader that maps class names to file paths using PSR-4 or classmap standards.
Code Example (composer.json snippet):
{
"autoload": {
"psr-4": {
"MyApp\\": "src/"
}
}
}
Run composer dump-autoload
after updating.
44. Debugging PHP with Xdebug
Question: How can Xdebug be used to debug PHP code?
Explanation: Xdebug provides stack traces, breakpoints, and profiling, making it easier to diagnose issues during development.
Configuration Example (php.ini):
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=1
45. Best Practices for Secure PHP Code
Question: What are some best practices for writing secure PHP applications?
Explanation:
- Use prepared statements to prevent SQL injection.
- Sanitize and validate all user input.
- Use HTTPS and secure session configurations.
- Keep PHP and dependencies up to date.
No specific code sample; these practices are integrated throughout your code.
46. Optimizing PHP Performance
Question: How can you optimize a PHP application’s performance?
Explanation:
- Use caching solutions (OPCache, APCu).
- Optimize database queries and indexing.
- Profile and refactor code using tools like Xdebug and Blackfire.
No specific code sample is provided.
47. The Role of PHP-FIG
Question: What is PHP-FIG and how has it impacted PHP development?
Explanation: PHP-FIG (Framework Interoperability Group) creates common standards (PSRs) that help improve code quality and interoperability between libraries and frameworks.
No code sample is required.
48. Implementing Middleware
Question: How do you implement middleware in a PHP application?
Explanation: Middleware acts as a pipeline where each component processes the request and optionally passes it to the next component.
Code Example:
<?php
interface Middleware {
public function handle($request, callable $next);
}
class AuthMiddleware implements Middleware {
public function handle($request, callable $next) {
if (!isset($request['user'])) {
return "Unauthorized";
}
return $next($request);
}
}
$request = ['user' => 'Alice'];
$middleware = new AuthMiddleware();
$response = $middleware->handle($request, function($req) {
return "Request passed to controller";
});
echo $response;
?>
49. Configuring error_reporting()
Question: How do you configure error reporting for different environments in PHP?
Explanation: You can set error reporting levels based on the environment (development vs. production) to display errors during development and hide them in production.
Code Example:
<?php
if (getenv('APP_ENV') === 'development') {
error_reporting(E_ALL);
ini_set('display_errors', '1');
} else {
error_reporting(0);
ini_set('display_errors', '0');
}
?>
50. Unit Testing with PHPUnit
Question: How do you write a simple unit test in PHP using PHPUnit?
Explanation: Unit tests help validate that individual parts of your code work as expected.
Code Example (Calculator.php):
<?php
class Calculator {
public function add($a, $b) {
return $a + $b;
}
}
Test (tests/CalculatorTest.php):
<?php
use PHPUnit\Framework\TestCase;
class CalculatorTest extends TestCase {
public function testAdd() {
$calc = new Calculator();
$this->assertEquals(4, $calc->add(2, 2));
}
}
?>
Run the test with:
vendor/bin/phpunit --bootstrap vendor/autoload.php tests
51. Include vs. Include_once vs. Require vs. Require_once
Question: What are the differences among these four file inclusion functions?
Explanation:
- include: Includes a file; emits a warning if not found.
- include_once: Same as include but prevents multiple inclusions.
- require: Includes a file; emits a fatal error if not found.
- require_once: Like require but ensures the file is included only once.
Code Example:
<?php
include 'config.php';
require_once 'functions.php';
?>
52. Null Coalescing Operator (??)
Question: How does the null coalescing operator work in PHP?
Explanation: The operator returns the left-hand operand if it exists and is not null; otherwise, it returns the right-hand operand.
Code Example:
<?php
$username = $_GET['user'] ?? 'guest';
echo "Hello, $username";
?>
53. The Spaceship Operator (<=>)
Question: How is the spaceship operator used in PHP?
Explanation: The spaceship operator is used for three-way comparisons, returning -1, 0, or 1.
Code Example:
<?php
echo 1 <=> 2; // -1
echo 2 <=> 2; // 0
echo 3 <=> 2; // 1
?>
54. Array Functions: array_map, array_filter, array_reduce
Question: How do you use these array functions?
Explanation:
- array_map: Applies a callback to each array element.
- array_filter: Filters elements using a callback.
- array_reduce: Reduces an array to a single value using a callback.
Code Example:
<?php
$numbers = [1, 2, 3, 4, 5];
// array_map:
$squared = array_map(function($n) { return $n * $n; }, $numbers);
// array_filter:
$evens = array_filter($numbers, function($n) { return $n % 2 === 0; });
// array_reduce:
$sum = array_reduce($numbers, function($carry, $n) { return $carry + $n; }, 0);
print_r($squared);
print_r($evens);
echo $sum;
?>
55. array_merge() vs. array_replace()
Question: What is the difference between these two functions?
Explanation:
- array_merge(): Merges arrays and reindexes numeric keys.
- array_replace(): Replaces values in the first array with values from subsequent arrays using keys.
Code Example:
<?php
$array1 = ['a', 'b'];
$array2 = ['c', 'd'];
print_r(array_merge($array1, $array2)); // ['a', 'b', 'c', 'd']
$arrayAssoc1 = ['a' => 1, 'b' => 2];
$arrayAssoc2 = ['b' => 3, 'c' => 4];
print_r(array_replace($arrayAssoc1, $arrayAssoc2)); // ['a' => 1, 'b' => 3, 'c' => 4]
?>
56. Using filter_var() for Validation
Question: How do you validate data using filter_var()?
Explanation: filter_var() filters and validates data using built-in filters.
Code Example:
<?php
$email = "test@example.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Valid email.";
} else {
echo "Invalid email.";
}
?>
57. Magic Methods **get() and **set()
Question: How do get() and **set() work?
**Explanation: These magic methods intercept property access, allowing you to define custom behavior when getting or setting inaccessible properties.
Code Example:
<?php
class Magic {
private $data = [];
public function __get($name) {
return $this->data[$name] ?? null;
}
public function __set($name, $value) {
$this->data[$name] = $value;
}
}
$obj = new Magic();
$obj->name = "Alice";
echo $obj->name;
?>
58. **sleep() and **wakeup() for Serialization
Question: How do sleep() and **wakeup() assist in serialization?
**Explanation: sleep() specifies which properties to serialize, while **wakeup() reinitializes properties upon unserialization.
**Code Example:
<?php
class Example {
public $data;
private $resource;
public function __sleep() {
return ['data']; // Do not serialize the resource
}
public function __wakeup() {
$this->resource = fopen('file.txt', 'r');
}
}
?>
59. Observer Pattern in PHP
Question: How can you implement the Observer pattern?
Explanation: The Observer pattern allows an object (subject) to notify a list of observers automatically of any state changes.
Code Example:
<?php
interface Observer {
public function update($data);
}
class ConcreteObserver implements Observer {
public function update($data) {
echo "Observer notified with data: " . $data;
}
}
class Subject {
private $observers = [];
public function attach(Observer $observer) {
$this->observers[] = $observer;
}
public function notify($data) {
foreach ($this->observers as $observer) {
$observer->update($data);
}
}
}
$subject = new Subject();
$subject->attach(new ConcreteObserver());
$subject->notify("New Event Occurred");
?>
60. Strategy Pattern in PHP
Question: How do you implement the strategy pattern?
Explanation: The strategy pattern defines a family of algorithms, encapsulating each one and making them interchangeable.
Code Example:
<?php
interface SortingStrategy {
public function sort(array $data): array;
}
class QuickSort implements SortingStrategy {
public function sort(array $data): array {
// Quick sort algorithm...
sort($data);
return $data;
}
}
class MergeSort implements SortingStrategy {
public function sort(array $data): array {
// Merge sort algorithm...
sort($data);
return $data;
}
}
class Sorter {
private $strategy;
public function __construct(SortingStrategy $strategy) {
$this->strategy = $strategy;
}
public function sort(array $data): array {
return $this->strategy->sort($data);
}
}
$data = [5, 3, 8, 1];
$sorter = new Sorter(new QuickSort());
print_r($sorter->sort($data));
?>
61. File System Operations with SPL Iterators
Question: How can you traverse directories using SPL Iterators?
Explanation: SPL provides iterators like RecursiveDirectoryIterator to traverse file systems.
Code Example:
<?php
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(__DIR__)
);
foreach ($iterator as $file) {
if ($file->isFile()) {
echo $file->getFilename() . "\n";
}
}
?>
62. Using DateTime and DateInterval
Question: How do you work with dates using DateTime and DateInterval?
Explanation: DateTime offers a robust API for handling dates and times, while DateInterval represents a time period.
Code Example:
<?php
$date = new DateTime('now');
$date->add(new DateInterval('P1D')); // Add one day
echo $date->format('Y-m-d');
?>
63. DateTimeImmutable Class
Question: What is DateTimeImmutable and how does it differ from DateTime?
Explanation: DateTimeImmutable objects cannot be modified after creation, ensuring that date changes return new objects.
Code Example:
<?php
$date = new DateTimeImmutable('now');
$newDate = $date->modify('+1 day');
echo $date->format('Y-m-d') . "\n"; // original date remains unchanged
echo $newDate->format('Y-m-d');
?>
64. Password Hashing Functions
Question: How do you use PHP’s password hashing functions?
Explanation: PHP provides built-in functions to hash and verify passwords securely.
Code Example:
<?php
$password = 'secret123';
$hash = password_hash($password, PASSWORD_DEFAULT);
if (password_verify('secret123', $hash)) {
echo "Password is valid!";
} else {
echo "Invalid password.";
}
?>
65. Creating a Simple Router for MVC
Question: How do you implement a basic router in PHP?
Explanation: A router maps incoming requests to the appropriate controller action based on the URL.
Code Example:
<?php
// routes.php
$routes = [
'/' => 'HomeController@index',
'/about' => 'PageController@about'
];
$requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if (isset($routes[$requestUri])) {
list($controller, $action) = explode('@', $routes[$requestUri]);
(new $controller)->$action();
} else {
http_response_code(404);
echo "Page not found.";
}
?>
66. Handling Command-Line Arguments in PHP CLI
Question: How do you work with command-line arguments in a PHP script?
Explanation: PHP CLI scripts can access arguments via the $argv
array.
Code Example:
<?php
// Run as: php script.php arg1 arg2
array_shift($argv); // Remove script name
print_r($argv);
?>
67. Building a CLI Application Using Symfony Console
Question: How do you create a CLI application using the Symfony Console component?
Explanation: Symfony Console provides tools to build rich CLI applications.
Code Example (snippet):
<?php
// composer.json should include "symfony/console"
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class HelloCommand extends Command {
protected static $defaultName = 'app:hello';
protected function execute(InputInterface $input, OutputInterface $output) {
$output->writeln("Hello from Symfony Console!");
return Command::SUCCESS;
}
}
$application = new Application();
$application->add(new HelloCommand());
$application->run();
?>
68. Implementing Custom Session Handlers
Question: How do you create a custom session handler in PHP?
Explanation: By implementing the SessionHandlerInterface, you can control how sessions are stored.
Code Example:
<?php
class MySessionHandler implements SessionHandlerInterface {
public function open($savePath, $sessionName) { return true; }
public function close() { return true; }
public function read($id) { return ''; }
public function write($id, $data) { return true; }
public function destroy($id) { return true; }
public function gc($maxlifetime) { return true; }
}
session_set_save_handler(new MySessionHandler(), true);
session_start();
$_SESSION['foo'] = 'bar';
?>
69. Using Memcached in PHP
Question: How do you integrate Memcached for caching?
Explanation: Memcached is an in-memory caching system that can speed up applications by caching data.
Code Example:
<?php
$memcache = new Memcached();
$memcache->addServer('localhost', 11211);
$key = 'cache_key';
$data = $memcache->get($key);
if ($data === false) {
$data = "This is cached data.";
$memcache->set($key, $data, 3600);
}
echo $data;
?>
70. Using Redis in PHP for Caching
Question: How can Redis be used for caching in PHP?
Explanation: Redis is a key-value store that supports advanced data structures for caching and more.
Code Example:
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$key = 'cache_key';
$data = $redis->get($key);
if (!$data) {
$data = "This is cached data.";
$redis->setex($key, 3600, $data);
}
echo $data;
?>
71. Implementing Logging with Monolog
Question: How do you use Monolog for logging in PHP?
Explanation: Monolog is a popular logging library that supports various handlers and formatters.
Code Example:
<?php
require 'vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$log = new Logger('app');
$log->pushHandler(new StreamHandler('app.log', Logger::WARNING));
$log->warning("This is a warning!");
$log->error("This is an error!");
?>
72. Event System Using Symfony EventDispatcher
Question: How do you build an event system using Symfony’s EventDispatcher?
Explanation: This component allows you to dispatch events and subscribe listeners.
Code Example:
<?php
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Contracts\EventDispatcher\Event;
class UserRegisteredEvent extends Event {
public $username;
public function __construct($username) {
$this->username = $username;
}
}
$dispatcher = new EventDispatcher();
$dispatcher->addListener(UserRegisteredEvent::class, function (UserRegisteredEvent $event) {
echo "User registered: " . $event->username;
});
$dispatcher->dispatch(new UserRegisteredEvent('Alice'));
?>
73. Using Guzzle for HTTP Requests
Question: How do you make HTTP requests using Guzzle?
Explanation: Guzzle is a PHP HTTP client that makes it simple to send HTTP requests and integrate with web services.
Code Example:
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
$response = $client->request('GET', 'https://api.example.com/data');
echo $response->getBody();
?>
74. Using SplFileObject
Question: How do you work with files using SplFileObject?
Explanation: SplFileObject provides an object-oriented interface for reading and writing files.
Code Example:
<?php
$file = new SplFileObject("example.txt", "r");
while (!$file->eof()) {
echo $file->fgets();
}
?>
75. File and Directory Manipulation
Question: How do you work with directories in PHP using functions like scandir()?
Explanation: PHP’s built-in functions allow you to open, read, and manipulate files and directories.
Code Example:
<?php
$files = scandir(__DIR__);
print_r($files);
?>
76. Using Regular Expressions (preg_match, preg_replace)
Question: How do you work with regex in PHP?
Explanation: PHP provides powerful functions to search and replace text using regular expressions.
Code Example:
<?php
if (preg_match('/^Hello/', 'Hello World')) {
echo "String starts with 'Hello'";
}
$text = preg_replace('/World/', 'PHP', 'Hello World');
echo $text; // "Hello PHP"
?>
77. Implementing a Custom Error Handler
Question: How do you create a custom error handler in PHP?
Explanation: Using set_error_handler()
, you can intercept errors and handle them in a centralized manner.
Code Example:
<?php
function myErrorHandler($errno, $errstr, $errfile, $errline) {
echo "Error [$errno]: $errstr in $errfile on line $errline";
}
set_error_handler("myErrorHandler");
// Trigger an error
echo $undefinedVar;
?>
78. Using Stream Contexts with file_get_contents()
Question: How do you use stream contexts in PHP?
Explanation: Stream contexts allow you to modify options and parameters when working with streams, such as HTTP requests.
Code Example:
<?php
$options = [
'http' => [
'method' => 'GET',
'header' => 'User-Agent: PHP'
]
];
$context = stream_context_create($options);
$data = file_get_contents('https://api.example.com/data', false, $context);
echo $data;
?>
79. Simple File-Based Caching Mechanism
Question: How can you implement a basic caching mechanism using files?
Explanation: Cache data by writing to and reading from files, reducing the need for repeated heavy computations.
Code Example:
<?php
$cacheFile = 'cache.txt';
if (file_exists($cacheFile) && (time() - filemtime($cacheFile)) < 3600) {
$data = file_get_contents($cacheFile);
} else {
$data = "This is fresh data.";
file_put_contents($cacheFile, $data);
}
echo $data;
?>
80. CRUD with PDO Prepared Statements
Question: How do you perform CRUD operations using PDO prepared statements?
Explanation: Prepared statements help protect against SQL injection and provide an efficient way to execute queries.
Code Example (Insert):
<?php
$pdo = new PDO("mysql:host=localhost;dbname=testdb", "user", "pass");
$stmt = $pdo->prepare("INSERT INTO users (name, email) VALUES (:name, :email)");
$stmt->execute(['name' => 'Alice', 'email' => 'alice@example.com']);
?>
81. Using SPL Exceptions
Question: How do you use SPL exceptions like LogicException or RuntimeException?
Explanation: SPL provides a set of standard exception classes to handle various error conditions.
Code Example:
<?php
function doSomething($param) {
if ($param < 0) {
throw new LogicException("Parameter cannot be negative.");
}
return $param;
}
try {
doSomething(-1);
} catch (LogicException $e) {
echo $e->getMessage();
}
?>
82. Implementing a Rate Limiter
Question: How do you implement a simple rate limiter in PHP?
Explanation: A rate limiter restricts the number of requests a client can make within a given time window.
Code Example:
<?php
session_start();
$limit = 5;
$window = 60; // seconds
if (!isset($_SESSION['requests'])) {
$_SESSION['requests'] = [];
}
$_SESSION['requests'] = array_filter($_SESSION['requests'], function($time) use ($window) {
return $time > (time() - $window);
});
if (count($_SESSION['requests']) >= $limit) {
die("Rate limit exceeded. Try again later.");
}
$_SESSION['requests'][] = time();
echo "Request processed.";
?>
83. Using Closures for a Callback Queue
Question: How do you use closures to create a simple callback queue?
Explanation: Closures can be stored in an array and invoked later to process a series of tasks.
Code Example:
<?php
$queue = [];
$queue[] = function() { echo "Task 1 executed\n"; };
$queue[] = function() { echo "Task 2 executed\n"; };
foreach ($queue as $callback) {
$callback();
}
?>
84. The __debugInfo() Magic Method
Question: What is the purpose of debugInfo()?
**Explanation: debugInfo() allows you to customize what gets output when using var_dump() on an object.
**Code Example:
<?php
class DebugExample {
private $data = 'secret';
public function __debugInfo() {
return ['data' => '***'];
}
}
$obj = new DebugExample();
var_dump($obj);
?>
85. Object Serialization and Unserialization
Question: How do you serialize and unserialize objects in PHP?
Explanation: Serialization converts an object into a storable representation, and unserialization restores it.
Code Example:
<?php
class User {
public $name;
}
$user = new User();
$user->name = "Alice";
$serialized = serialize($user);
$newUser = unserialize($serialized);
echo $newUser->name;
?>
86. IteratorAggregate Interface
Question: How do you implement custom iteration using IteratorAggregate?
Explanation: Implementing IteratorAggregate allows an object to be iterated over by returning an external iterator.
Code Example:
<?php
class Collection implements IteratorAggregate {
private $items;
public function __construct(array $items) {
$this->items = $items;
}
public function getIterator() {
return new ArrayIterator($this->items);
}
}
$collection = new Collection([1, 2, 3]);
foreach ($collection as $item) {
echo $item . " ";
}
?>
87. Sanitizing Input with filter Extension
Question: How do you sanitize inputs using PHP’s filter extension?
Explanation: Use functions like filter_input() to sanitize and validate user input.
Code Example:
<?php
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
echo $name;
?>
88. Configuring PHP Error Levels
Question: What are E_NOTICE, E_WARNING, etc., and how do you configure them?
Explanation: PHP’s error levels control which errors are reported; you can configure these via error_reporting().
Code Example:
<?php
error_reporting(E_ALL & ~E_NOTICE);
?>
89. Implementing a Plugin System
Question: How do you build a simple plugin system in PHP?
Explanation: A plugin system can load and execute external modules (plugins) that adhere to a common interface.
Code Example:
<?php
interface Plugin {
public function run();
}
class PluginManager {
private $plugins = [];
public function register(Plugin $plugin) {
$this->plugins[] = $plugin;
}
public function runPlugins() {
foreach ($this->plugins as $plugin) {
$plugin->run();
}
}
}
class SamplePlugin implements Plugin {
public function run() {
echo "Plugin executed.\n";
}
}
$manager = new PluginManager();
$manager->register(new SamplePlugin());
$manager->runPlugins();
?>
90. Multithreading with pthreads (Experimental)
Question: How can you implement multithreading in PHP using pthreads?
Explanation: The pthreads extension allows for parallel execution but is experimental and only works in CLI.
Code Example:
<?php
// Note: Requires pthreads extension and CLI SAPI.
class WorkerThread extends Thread {
public function run() {
echo "Thread running\n";
}
}
$thread = new WorkerThread();
$thread->start();
$thread->join();
?>
91. Asynchronous Programming with Promises
Question: How can you use asynchronous programming in PHP with promises?
Explanation: Libraries like ReactPHP implement promises to handle asynchronous tasks.
Code Example (conceptual):
<?php
// Using ReactPHP Promise library
use React\Promise\Promise;
$promise = new Promise(function ($resolve, $reject) {
// Asynchronous operation
sleep(1);
$resolve("Async result");
});
$promise->then(function($result) {
echo $result;
});
?>
92. Building a Basic WebSocket Server with Ratchet
Question: How do you create a simple WebSocket server in PHP using Ratchet?
Explanation: Ratchet is a library for real-time, bi-directional applications between clients and servers over WebSockets.
Code Example:
<?php
// Run "composer require cboden/ratchet" before using.
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
public function onOpen(ConnectionInterface $conn) { echo "New connection\n"; }
public function onMessage(ConnectionInterface $from, $msg) { $from->send("You said: $msg"); }
public function onClose(ConnectionInterface $conn) { echo "Connection closed\n"; }
public function onError(ConnectionInterface $conn, \Exception $e) { $conn->close(); }
}
?>
Run the server using Ratchet’s application runner.
93. Securing Against XSS
Question: How do you prevent Cross-Site Scripting (XSS) attacks in PHP?
Explanation: Always sanitize output using functions like htmlspecialchars() before rendering user input in the browser.
Code Example:
<?php
$userInput = '<script>alert("XSS")</script>';
echo htmlspecialchars($userInput, ENT_QUOTES, 'UTF-8');
?>
94. Securing Against CSRF
Question: How do you protect your PHP application from CSRF attacks?
Explanation: Generate and validate CSRF tokens for forms to ensure that requests originate from legitimate sources.
Code Example:
<?php
// Generating a CSRF token
session_start();
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
?>
<form method="post">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
<!-- form fields -->
</form>
<?php
// On form submission, validate the token
if ($_POST['csrf_token'] !== $_SESSION['csrf_token']) {
die("Invalid CSRF token");
}
?>
95. Using SplPriorityQueue
Question: How do you use the SplPriorityQueue in PHP?
Explanation: SplPriorityQueue allows you to store elements with priorities, and extract them in order of priority.
Code Example:
<?php
$queue = new SplPriorityQueue();
$queue->insert("Low priority", 1);
$queue->insert("High priority", 3);
$queue->insert("Medium priority", 2);
while (!$queue->isEmpty()) {
echo $queue->extract() . "\n";
}
?>
96. Building a Basic Dependency Container
Question: How do you implement a simple dependency injection container?
Explanation: A dependency container manages object creation and wiring, allowing for easier management of dependencies.
Code Example:
<?php
class Container {
private $bindings = [];
public function bind($abstract, callable $factory) {
$this->bindings[$abstract] = $factory;
}
public function make($abstract) {
return $this->bindings[$abstract]($this);
}
}
$container = new Container();
$container->bind('logger', function() {
return new Monolog\Logger('app');
});
$logger = $container->make('logger');
?>
97. Caching Decorator Pattern
Question: How do you implement a caching decorator to cache method results?
Explanation: A caching decorator wraps a class and caches the results of its method calls to improve performance.
Code Example:
<?php
interface DataProvider {
public function getData();
}
class ExpensiveDataProvider implements DataProvider {
public function getData() {
// Simulate expensive operation
sleep(1);
return "Expensive Data";
}
}
class CachingDecorator implements DataProvider {
private $provider;
private $cache;
public function __construct(DataProvider $provider) {
$this->provider = $provider;
}
public function getData() {
if (!$this->cache) {
$this->cache = $this->provider->getData();
}
return $this->cache;
}
}
$provider = new CachingDecorator(new ExpensiveDataProvider());
echo $provider->getData();
?>
98. Using function_exists() and class_exists()
Question: How do you conditionally load functions or classes in PHP?
Explanation: Use these functions to check if a function or class is already defined to avoid redefinition errors.
Code Example:
<?php
if (!function_exists('myFunction')) {
function myFunction() {
return "Function defined!";
}
}
if (!class_exists('MyClass')) {
class MyClass {
public function greet() {
return "Hello!";
}
}
}
echo myFunction();
$obj = new MyClass();
echo $obj->greet();
?>
99. Robust Logging Mechanism with Error Levels
Question: How can you implement a robust logging mechanism with different error levels?
Explanation: Use logging libraries (like Monolog) or create a custom logger that categorizes logs (info, warning, error).
Code Example:
<?php
class SimpleLogger {
const LEVEL_INFO = 'INFO';
const LEVEL_ERROR = 'ERROR';
public function log($level, $message) {
echo "[" . $level . "] " . $message . "\n";
}
}
$logger = new SimpleLogger();
$logger->log(SimpleLogger::LEVEL_INFO, "This is an informational message.");
$logger->log(SimpleLogger::LEVEL_ERROR, "This is an error message.");
?>
100. Using Reflection to Invoke Private Methods
Question: How do you use PHP’s Reflection API to invoke private methods?
Explanation: Reflection can bypass visibility restrictions, allowing you to call private or protected methods (commonly used in testing).
Code Example:
<?php
class Secret {
private function whisper() {
return "Secret Message";
}
}
$secret = new Secret();
$reflection = new ReflectionClass($secret);
$method = $reflection->getMethod('whisper');
$method->setAccessible(true);
echo $method->invoke($secret);
?>
This list of 100 PHP Interview Questions covers a wide spectrum of topics—from language fundamentals and object-oriented programming to advanced concepts like design patterns, security, performance optimizations, and modern PHP practices. Use these examples as a reference and a starting point for deepening your understanding of PHP’s capabilities in a professional setting.
13. Describe the $_GET, _$_POST, and $_REQUEST superglobal arrays.
Each of these superglobal arrays in PHP helps manage input data, but they have distinct characteristics and use-cases.
Key Features
- $_GET is URL-based. It extracts data from the query string. In other words, data is visible in the URL.
- $_POST is form-based. It’s suitable for handling sensitive or large data as it’s not visible in the URL.
- $_REQUEST is a combination of $_GET, $_POST, and $_COOKIE. If a parameter is accessible in multiple arrays, $_REQUEST uses the one with the highest precedence. However, its use is largely depreciated because it makes debugging and code maintenance more difficult. It’s better to be specific by using $_GET or $_POST where applicable.
<br>
14. How can you prevent form submission data from being injected with malicious code?
To prevent cross-site scripting (XSS) attacks on your website, it is crucial to validate and sanitize any data submitted through forms.
Key Anti-XSS Techniques
Manual Escaping
Escape form data using htmlspecialchars
to convert special characters to HTML entities.
echo htmlspecialchars($_POST['input']);
JavaScript Sanitization
To prevent execution of JavaScript code, you can use:
-
JavaScript replace method: Replace the less-than and greater-than characters with their HTML entities.
$sanitized = str_replace(['<', '>'], ['<', '>'], $_POST['input']);
-
JSON encoding for non-text data in hidden fields.
$jsonEncoded = json_encode($_POST['data']);
Safe Back-End Handling
Always perform thorough server-side validation and ensure only intended actions are executed in response to form submissions:
- Database Prepared Statements: Use prepared statements alongside parameterized queries when interfacing with the database.
- Strict Input Validation: Enforce strict criteria for input data. For instance, use
filter_var
for emails or regex for defined patterns. - Context-Aware Processing: Differentiate how the input will be used (e.g., in an email, as file content), and process accordingly.
Security Libraries
Frameworks and libraries often provide dedicated modules to fortify against XSS threats. For instance, Laravel supports various middlewares such as VerifyCsrfToken, which especially help in guarding against CSRF attacks.
Code
Here is the PHP code:
// Using htmlspecialchars for basic output
echo htmlspecialchars($_POST['input']);
// Using JSON to encode data going into hidden fields
$jsonEncoded = json_encode($_POST['data']);
// Using prepared statements for database queries
$stmt = $dbh->prepare("SELECT * FROM users WHERE username=?");
$stmt->execute([$_POST['username']]);
// Context-aware input verification
$filterOptions = [
"email" => [
"filter" => FILTER_VALIDATE_EMAIL,
"flags" => FILTER_FLAG_EMAIL_UNICODE
]
];
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL, $filterOptions);
15. What is the significance of “htmlspecialchars” and “striptags” in _PHP?
Both htmlspecialchars and strip_tags are crucial PHP functions that enhance security by mitigating Cross-Site Scripting (XSS) risks. They play specialized roles, catering to different requirements within web applications.
htmlspecialchars
The primary purpose of htmlspecialchars
is to sanitize user input to render it harmless when displaying it on a web page. It achieves this by converting special characters into their respective HTML entities. By doing so, it prevents the accidental or unauthorized execution of HTML, JavaScript, or CSS, maintaining data integrity.
For instance, ‘<’ is converted to "<"
, ‘>’ to ">"
, ‘&’ to "&"
, and quotes to their respective entity representations.
strip_tags
The comparative task of strip_tags
is somewhat more brute-force. It’s designed to remove any HTML and PHP tags from the input. This is a potential security risk and is often discouraged, but it might be suitable when an application needs bare-bones, text-only input.
Developers can further refine strip_tags
by specifying allowable tags or attributes. However, it’s still a less precise method compared to htmlspecialchars
with its exact handling of special characters.
Best Practices for Security
For optimal data and user security, utilizing both functions is often the most recommended approach. This multi-layered strategy ensures that dangerous input goes through extensive sanitation measures.
When integrating user-generated content, especially in HTML contexts, it’s crucial never to solely rely on strip_tags
. Balancing both subtlety and thoroughness, htmlspecialchars
is the more suitable choice in such scenarios.
Code Example: Multi-Layered Sanitization
Here is the PHP code:
$input = "<a href='#'>Malicious Link</a><script>alert('You have been hacked!')</script>";
$clean_html = htmlspecialchars($input, ENT_QUOTES, 'UTF-8');
$clean_text = strip_tags($input);
echo "Clean HTML: $clean_html\n"; // Outputs: <a href='#'>Malicious Link</a><script>alert('You have been hacked!')</script>
echo "Clean Text: $clean_text\n"; // Outputs: Malicious Linkalert('You have been hacked!')
16. PHP Interview Questions and Answers
Table of Contents
Language Features
1. Data types in PHP. Describe Resource and Callable types.
Answer:
PHP provides several data types, including scalar, compound, and special types. Resource
is a special type that holds references to external resources like database connections or file handles. Callable
represents a reference to a function or method.
Example:
$resource = fopen("test.txt", "w"); // Resource type
$callable = function() { return "Hello"; }; // Callable type
var_dump($resource);
var_dump(is_callable($callable));
2. this
VS self
, self
VS static
, parent
VS self
Answer:
this
refers to the current object instance.self
refers to the class where it is used.static
enables late static binding, referring to the called class.parent
calls a method from the parent class.
Example:
class Base {
public function test() {
return self::class;
}
}
class Child extends Base {
public function test() {
return parent::test();
}
}
echo (new Child)->test();
3. Magic constants list. Does magic constant’s value depend on the place where it is called?
Answer:
Yes, magic constants like __LINE__
, __FILE__
, and __CLASS__
depend on where they are called.
Example:
echo __LINE__; // Outputs current line number
4. Generators usage examples. What makes a function become a generator?
Answer:
A function using yield
becomes a generator.
Example:
function generatorExample() {
yield 1;
yield 2;
}
$gen = generatorExample();
foreach ($gen as $val) echo $val; // Outputs 12
Traits
1. How to add them, inherit, and instantiate traits?
Answer: Traits allow code reuse across multiple classes.
Example:
trait MyTrait { public function sayHello() { echo "Hello"; } }
class MyClass { use MyTrait; }
echo (new MyClass)->sayHello();
PHP Versions
1. New features in PHP 8.0
Answer: PHP 8.0 introduced named arguments, JIT compilation, union types, and match expressions.
Example:
function greet(string $name) { return "Hello, $name"; }
echo greet(name: "John");
Tricky Questions
1. What will be the key for the next value in this array?
$array = [0 => "A", 1 => "B", "Hello" => "C"];
$array[] = "D"; // Next key?
print_r($array);
Answer: The next key will be 2
.
2. How to find the second-largest number in an unsorted list using one iteration?
Example:
function secondLargest($arr) {
$max = $second = PHP_INT_MIN;
foreach ($arr as $num) {
if ($num > $max) {
$second = $max;
$max = $num;
} elseif ($num > $second && $num != $max) {
$second = $num;
}
}
return $second;
}
Testing and Running Code
Running PHP Code Locally
- Install PHP (https://www.php.net/downloads)
- Create a
test.php
file and write your code inside it. - Run the file in the terminal:
php test.php
Running Code Online
Use platforms like:
- https://www.phptester.net/
- https://3v4l.org/
This README provides a structured approach to PHP questions and their practical answers with executable code snippets.