PHP Interview Questions

PHP Interview Questions and Answers for Intermediate Developers

Review intermediate PHP interview topics including OOP, exceptions, namespaces, Composer, PDO, HTTP, testing, and performance.

Intermediate PHP Interview Questions

These questions explore practical application development, maintainability, and runtime behavior.

1. What are the main principles of object-oriented programming?

Commonly discussed principles are encapsulation, abstraction, inheritance, and polymorphism. They help organize behavior and define contracts, but should be applied where they simplify the design.

2. What is the difference between an interface and an abstract class?

An interface defines a contract that implementing classes must fulfill. An abstract class can provide shared implementation and state while leaving some methods for subclasses to implement. A class can implement multiple interfaces but can extend only one class.

3. What are namespaces in PHP?

Namespaces organize classes, interfaces, traits, functions, and constants and help prevent naming collisions.

namespace App\Services;

class Mailer
{
}

A class can be referenced with its fully qualified name or imported with use.

4. What is Composer autoloading?

Composer generates an autoloader based on project configuration and installed packages. PSR-4 autoloading maps namespace prefixes to directories, reducing the need for manual require statements.

5. How do exceptions work?

An exception represents an exceptional condition that can be thrown and handled with try, catch, and optionally finally.

try {
    $result = $service->process();
} catch (DomainException $exception) {
    error_log($exception->getMessage());
    http_response_code(422);
}

Catch exceptions where the application can take a meaningful action. Avoid silently swallowing failures.

6. What is dependency injection?

Dependency injection supplies an object with the collaborators it needs instead of having it create them internally. Constructor injection makes dependencies explicit and often improves testability.

final class ArticleService
{
    public function __construct(
        private ArticleRepository $articles
    ) {
    }
}

7. How do PDO transactions work?

A transaction groups database operations so they can be committed together or rolled back if an operation fails.

$pdo->beginTransaction();

try {
    // Execute related statements.
    $pdo->commit();
} catch (Throwable $exception) {
    if ($pdo->inTransaction()) {
        $pdo->rollBack();
    }
    throw $exception;
}

Use a transactional storage engine such as InnoDB.

8. What is the difference between PUT, PATCH, and POST?

POST commonly submits data for processing or creates subordinate resources. PUT conventionally replaces the target resource representation and is idempotent. PATCH applies a partial modification. Exact behavior depends on the API contract.

9. What is output buffering?

Output buffering captures output instead of sending it immediately. Functions such as ob_start() and ob_get_clean() can be useful for rendering templates, but buffering should not be used to hide incorrect header or output flow.

10. What is OPcache?

OPcache stores precompiled PHP bytecode in shared memory, reducing repeated parsing and compilation. It is commonly enabled in production and should be configured according to the deployment model.

11. How would you test a PHP class?

Test observable behavior with focused unit tests. Mock external dependencies only when useful, and add integration tests for database and HTTP boundaries. Tests should cover normal cases, invalid input, and failure paths.

12. How do you handle configuration for different environments?

Keep environment-specific values outside source code, load them through a central configuration layer, and never commit secrets. Validate required configuration during application startup.

13. What is the difference between array_map() and array_filter()?

array_map() transforms values, while array_filter() keeps values that satisfy a predicate. array_filter() preserves keys unless they are explicitly reindexed.

14. How do you improve a slow database-backed page?

Measure first. Inspect query plans, reduce unnecessary columns and repeated queries, add appropriate indexes, paginate large result sets, and cache only data that can safely be reused.

15. What is a race condition?

A race condition occurs when the result depends on the timing or interleaving of concurrent operations. Database transactions, constraints, atomic updates, and row-level locking can help protect shared state.

Share this page Help other developers discover this resource.