PHP Interview Questions

Advanced PHP Interview Questions and Answers

Prepare for advanced PHP interviews with questions about architecture, concurrency, security, runtime behavior, database consistency, and scalability.

Advanced PHP Interview Questions

These questions focus on trade-offs and system design. Strong answers should explain not only what a feature does, but when and why it should be used.

1. How would you design a PHP application for horizontal scaling?

Keep application instances stateless where practical. Store shared state in external systems such as a database, shared cache, or queue. Use centralized session storage if sessions must be shared, shared file/object storage for uploads, and health checks behind a load balancer.

2. How do you prevent duplicate processing of a request?

Use idempotency keys for operations that may be retried, enforce uniqueness in the database, and make state transitions atomic. Do not rely only on disabling a submit button in the browser.

3. What is the difference between optimistic and pessimistic locking?

Optimistic locking detects conflicting updates, often with a version column checked during update. Pessimistic locking acquires a lock before changing a record, for example with SELECT ... FOR UPDATE inside a transaction. Choose based on contention and consistency requirements.

4. How would you design reliable background jobs?

Persist jobs before acknowledging the request, process them with workers, retry transient failures with limits and backoff, and make handlers idempotent. Add dead-letter handling, observability, and a way to inspect or replay failed jobs safely.

5. What is the N+1 query problem?

It occurs when code executes one query to fetch a collection and then another query for each item. Solve it by fetching related data in batches, using joins where appropriate, or applying a deliberate eager-loading strategy.

6. How do you manage cache invalidation?

Define what data is cached, who owns invalidation, and how stale the result may be. Common approaches include time-to-live, explicit invalidation after writes, versioned keys, and event-driven invalidation. Consider race conditions and cache stampedes.

7. What are PHP Fibers?

Fibers provide suspendable execution contexts that can be paused and resumed. They are low-level primitives used by libraries and runtimes to build cooperative concurrency; they do not automatically make blocking I/O non-blocking.

8. What is the difference between concurrency and parallelism?

Concurrency means multiple tasks can make progress during overlapping periods. Parallelism means tasks execute at the same time on separate execution resources. Traditional PHP request handling is often process-based; architecture and runtime determine the available model.

9. How would you prevent SSRF in a URL-fetching feature?

Prefer a server-controlled destination map. If arbitrary destinations are unavoidable, enforce strict host allowlists, validate resolved IPv4 and IPv6 addresses, block private and link-local ranges, disable or revalidate redirects, restrict protocols, set timeouts, and apply outbound network controls.

10. How do you design a secure multi-tenant data model?

Include tenant ownership in relevant records and enforce tenant scope in every read and write path. Use composite constraints where appropriate, derive tenant identity from authenticated server-side context, and test cross-tenant access explicitly. A missing filter in one endpoint can expose another tenant's data.

11. How do you approach database schema changes with zero or low downtime?

Use backward-compatible expand-and-contract changes: add new nullable fields or tables first, deploy code that can handle both shapes, backfill data in batches, switch reads and writes, then remove obsolete structures in a later release. Plan rollback and monitor each phase.

12. What is the purpose of a Content Security Policy?

CSP lets a site define which sources the browser may load or execute. A carefully designed policy can reduce the impact of some injection flaws, but it does not replace output encoding, safe DOM APIs, or authorization checks.

13. How would you investigate a memory leak in a long-running PHP worker?

Track memory over many jobs, inspect retained references and large data structures, check resource cleanup, and reproduce with representative workloads. PHP's request lifecycle may hide problems that become visible in persistent workers.

14. What is the difference between process-level and application-level caching?

Process-level caching may be local to a PHP worker or runtime instance. Application-level shared caching is accessible across instances, depending on the backend. Consider consistency, invalidation, deployment behavior, and whether cached data is user-specific.

15. How would you design observability for a PHP service?

Use structured logs, request or trace identifiers, metrics for latency and error rates, and distributed tracing where the system spans services. Avoid logging secrets and personal data. Define alerts around user-impacting symptoms rather than noisy individual events.

16. How do you make a payment or order endpoint resilient to retries?

Require an idempotency key, persist the operation state, enforce database uniqueness, and coordinate external payment calls with a durable workflow. Model intermediate states and reconcile uncertain outcomes rather than blindly repeating a charge.

17. What is the difference between authentication, authorization, and accounting?

Authentication establishes identity, authorization decides permitted actions, and accounting/auditing records what happened. All three are important in administrative and regulated workflows.

18. How would you review a PHP application for security?

Map entry points and trust boundaries, review authentication and authorization, inspect SQL and output handling, test CSRF and upload paths, examine dependency advisories and server configuration, and verify logging, backup, and incident-response practices. Prioritize findings by exploitability and impact.

Share this page Help other developers discover this resource.