Interview Q&A · questions with model answers
PHP 8 Interview Questions
Ordered by how often they're asked. Most-asked answers are free to read in full; members see every answer and can rate their confidence on each.
-
Most asked How do you prevent SQL injection in PHP?
Use prepared statements with bound parameters (PDO or mysqli), never concatenate user input into SQL, and whitelist anything that can't be parameterized such as column names or sort direction.
-
Most asked How do you prevent XSS in PHP applications?
Escape output for its context: htmlspecialchars($s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') for HTML, json_encode with hex flags for JS, rawurlencode for URLs. Add a Content-Security-Policy as defense…
-
Most asked How do you protect a PHP app against CSRF and session attacks?
Use per-session CSRF tokens on state-changing requests, SameSite cookies, session_regenerate_id(true) on login, and secure cookie flags (Secure, HttpOnly).
-
How do you tune PHP-FPM process management?
Pick a process manager (static, dynamic or ondemand) and size pm.max_children from available RAM divided by average worker memory, then tune spare servers and pm.max_requests.
-
How do you write unit tests in PHP with PHPUnit or Pest?
PHPUnit test classes extend TestCase, with methods marked #[Test] (or prefixed test), and use assertions like assertSame. Pest offers a functional syntax (it('...', fn() => expect(...)->toBe(...)))…
-
How do you use transactions safely with PDO?
Call beginTransaction(), run your statements, commit() on success and rollBack() in a catch, with ERRMODE_EXCEPTION enabled so failures throw.
-
How would you add caching to a PHP application?
Layer caches: OPcache for code, APCu for per-server data, Redis/Memcached for shared application data (PSR-6/PSR-16), HTTP caching (ETag, Cache-Control) for responses, and a CDN for static assets.
-
How do you build a robust JSON API endpoint in plain PHP 8?
Decode input with json_decode(..., flags: JSON_THROW_ON_ERROR), validate it into a DTO, return proper status codes, and encode responses with json_encode(..., JSON_THROW_ON_ERROR) and Content-Type:…
-
How do you run background jobs in PHP?
Push work onto a queue (Redis, RabbitMQ, SQS, database) and process it with long-running worker processes such as Laravel Queue workers or Symfony Messenger consumers, supervised by systemd or…
-
How would you diagnose a slow PHP endpoint?
Measure first: reproduce with timing, profile with Xdebug/Blackfire/Tideways/SPX, check DB query counts and slow query logs, and inspect external calls, before optimizing the hotspot.
-
How would you plan an upgrade from PHP 7.4 to PHP 8.3+?
Inventory dependencies, run static analysis and Rector with PHP 8 rule sets, fix deprecations under the new version in CI, run the full test suite, then roll out gradually while monitoring errors.
-
How would you implement the Strategy pattern in modern PHP?
Define an interface for the varying behavior, implement one class per strategy, and select or inject the right implementation, often via a match on an enum or DI tagging.
-
How do you make HTTP requests from PHP reliably?
Use a PSR-18 client such as Guzzle or Symfony HttpClient with explicit timeouts, retries with backoff for idempotent requests, and proper error handling for 4xx/5xx responses.
-
How do you implement logging and observability in PHP?
Log through a PSR-3 logger (Monolog) with structured context, ship logs to a central system, and add metrics and tracing (OpenTelemetry PHP) for production visibility.
-
How do you containerize a PHP 8 application for production?
Use an official php:8.x-fpm (or FrankenPHP) image, install extensions with docker-php-ext-install, use a multi-stage build to run Composer, copy only production files, and run behind Nginx/Caddy…
-
How do you write robust PHP CLI scripts?
Use a console framework (Symfony Console or Laravel Artisan) for arguments, options, help and output, return proper exit codes, handle signals, and avoid web-only assumptions.
-
How do you control how objects are serialized to JSON?
Implement JsonSerializable::jsonSerialize() to return the data to encode, or use a serializer (Symfony Serializer, JMS, API Platform) with groups and normalizers for complex cases.
-
How would you implement rate limiting for a PHP API?
Track request counts per client key (IP, user, API key) in a shared store like Redis using a fixed window, sliding window or token bucket algorithm, and return HTTP 429 with Retry-After when exceeded.