PHP & Laravel Interview Questions and Answers
103 PHP & Laravel interview questions across 10 topics: PHP Core Fundamentals, OOP & Design Patterns in PHP, Modern PHP 8.x Features, Laravel Core Architecture & Lifecycle, Eloquent ORM & Database Design, RESTful APIs & Authentication, Security, OWASP & Hardening, Queues, Jobs & Background Workers, Caching & Performance Optimization, and Testing & Quality Assurance. They cover Junior, Senior, and Mid-level levels, and every question has a short answer to give in the interview, a deeper explanation, common mistakes, and links to official documentation with a last-reviewed date.
Topics
PHP Core Fundamentals
10 questions
OOP & Design Patterns in PHP
12 questions
Modern PHP 8.x Features
10 questions
Laravel Core Architecture & Lifecycle
11 questions
Eloquent ORM & Database Design
10 questions
RESTful APIs & Authentication
10 questions
Security, OWASP & Hardening
10 questions
Queues, Jobs & Background Workers
10 questions
Caching & Performance Optimization
10 questions
Testing & Quality Assurance
10 questions
PHP Core Fundamentals Interview Questions (PHP & Laravel)
- What is the difference between Type Juggling and declare(strict_types=1) in PHP?
- What is the difference between the == (Equal) and === (Identical) comparison operators in PHP 8+?
- How do OPcache and the JIT (Just-In-Time) compiler added in PHP 8 work?
- How does PHP manage memory with Reference Counting and cycle collection (Cycles Collection)?
- What is the difference between the Null Coalescing operator (??) and the Nullsafe Operator (?->) in modern PHP?
- What are Generators, and how does yield reduce memory use when reading large amounts of data?
- How has the error and exception hierarchy under Throwable evolved in modern PHP?
- How do PHP Sessions work, what is Session Locking, and how do you solve it?
- How do Closures differ from Arrow Functions (fn()) in PHP?
- How does PHP-FPM work, and how do its process management modes—static, dynamic, and ondemand—differ?
OOP & Design Patterns in PHP Interview Questions (PHP & Laravel)
- How does Constructor Property Promotion reduce boilerplate in PHP 8+ classes?
- What are Readonly Classes in PHP 8.2, and how do they support immutable Value Objects?
- How do you resolve method name conflicts when using multiple PHP Traits?
- What are the main PHP Magic Methods, and what do __get, __set, and __call do?
- When should you choose an Interface versus an Abstract Class in PHP code design?
- What is the essential difference between self and static in PHP Late Static Binding calls?
- What are Anonymous Classes, and when are they used in PHP?
- How do __invoke and __clone work, and what is a Deep Copy?
- How does passing objects to PHP functions differ from passing ordinary variables by reference?
- How do Service Containers use PHP's Reflection API to provide Auto-wiring?
- What is the Action-Domain-Responder (ADR) pattern, and how does it solve Fat Controllers?
- What are the pros and cons of using the Repository Pattern on top of Eloquent in Laravel?
Modern PHP 8.x Features Interview Questions (PHP & Laravel)
- What are the advantages of the match expression in PHP 8 over the traditional switch statement?
- What are Attributes in PHP 8+, and how are they better than older PHPDoc annotations?
- How do Enums work in PHP 8.1, and how do Pure Enums differ from Backed Enums?
- What are Named Arguments in PHP 8, and how are they useful with functions that have many options?
- What are Fibers in PHP 8.1, and how do they enable Async Concurrency?
- What is the difference between Union Types and Intersection Types in PHP 8+?
- What is the First-Class Callables syntax (...) in PHP 8.1, and how does it improve callbacks?
- What are the modern PHP 8 string functions str_contains, str_starts_with, and str_ends_with?
- What is FrankenPHP, and how does Worker Mode change the traditional way PHP applications run?
- What is the #[\Override] Attribute added in PHP 8.3, and how does it protect inheritance?
Laravel Core Architecture & Lifecycle Interview Questions (PHP & Laravel)
- What is the Laravel Request Lifecycle from arrival at public/index.php to the response being sent?
- How do bind, singleton, and scoped differ in Laravel's Service Container?
- How do register() and boot() differ in Laravel Service Providers, and when should each be used?
- How do Laravel Facades work internally, and what is the role of getFacadeAccessor?
- How does Laravel Octane improve application performance, and what are the risks of in-memory State Leaks?
- How does Laravel implement its Middleware Pipeline, and what is the TerminableMiddleware interface?
- Why should you never call env() outside config/ files in Laravel projects?
- How do regular Event Listeners differ from Event Subscribers in Laravel?
- How do you create custom Artisan commands and schedule them with Laravel Task Scheduler?
- How did Laravel 11 simplify application startup by removing the Http Kernel and moving configuration into bootstrap/app.php?
- What architectural requirements make a Laravel application Stateless and horizontally scalable (Horizontal Scaling)?
Eloquent ORM & Database Design Interview Questions (PHP & Laravel)
- What is the N+1 problem in Eloquent queries, and how do you eliminate it with Eager Loading?
- How do Polymorphic Relationships work in Eloquent, and what are the risks of storing class names?
- How do Local Scopes and Global Scopes differ in Eloquent models, and how do you implement them?
- How has Laravel's casting system developed, and how does the new casts() method differ from the $casts array?
- Compare chunk, chunkById, lazy, and cursor when processing millions of Eloquent records.
- How do you efficiently perform bulk inserts and updates (Upsert) in Eloquent with one query?
- What are Eloquent model events, and how do Observers organize business logic?
- How do Soft Deletes work in Laravel, and how do they affect database indexes?
- How do you manage database transactions with DB::transaction and handle Deadlocks?
- When should you use DB::raw, and what security precautions prevent SQL Injection?
RESTful APIs & Authentication Interview Questions (PHP & Laravel)
- What are API Resources in Laravel, and why are they better than returning Eloquent models directly as JSON?
- How do you isolate validation and authorization logic in Laravel Form Request classes?
- What are the best strategies for API Versioning in Laravel systems?
- How do you design advanced Rate Limiting with Laravel's RateLimiter class?
- What are the best practices for choosing HTTP Status Codes in a RESTful API?
- What is the main difference between LengthAwarePagination and CursorPagination in APIs?
- How do you design centralized, consistent error handling for Laravel API responses?
- How do you implement Idempotency Keys in Laravel payment and order creation APIs?
- Compare Laravel Sanctum and Laravel Passport, and when should you choose each one?
- How do you configure CORS in config/cors.php and avoid common errors with SPA applications?
Security, OWASP & Hardening Interview Questions (PHP & Laravel)
- How does CSRF protection work in Laravel, what causes a 419 Page Expired error, and how do you fix it?
- What is Mass Assignment, and how do $fillable and $guarded differ in Eloquent?
- How does Laravel hash passwords with the Hash Facade?
- How do Gates and Policies differ in Laravel authorization?
- How do Eloquent and Query Builder protect Laravel applications from SQL Injection?
- How does Blade protect against XSS, and what is the difference between {{ $data }} and {!! $data !!}?
- What are the strict security rules for uploading and storing files in Laravel?
- What is the role of APP_KEY in Laravel, and what are the risks of changing or exposing it in production?
- What are Signed URLs in Laravel, and when are they used to protect sensitive operations?
- How do you implement a Content Security Policy with a Nonce in Laravel applications?
Queues, Jobs & Background Workers Interview Questions (PHP & Laravel)
- How do Queues work in Laravel, and what is the purpose of the ShouldQueue interface?
- What is the main difference between php artisan queue:work and queue:listen?
- How does Laravel handle failed jobs, and what are the roles of failed() and Exponential Backoff?
- How do Job Chaining and Job Batching differ in Laravel?
- What is Laravel Horizon, and what does it offer for monitoring and managing Redis queues?
- How do you handle Stuck Jobs, and how does a job's $timeout differ from a worker's --timeout?
- How do you design multiple Laravel queues with different priorities (High, Default, Low)?
- How do you prevent the same job from being queued more than once with Laravel's ShouldBeUnique interface?
- How do you use Job Middleware to manage external API calls and rate limits?
- How do you configure Supervisor on Linux servers to keep Laravel workers running?
Caching & Performance Optimization Interview Questions (PHP & Laravel)
- Compare Laravel Cache Drivers: file, database, redis, and memcached.
- How does Cache::remember work, and how do Atomic Locks protect a server?
- How do you use Cache Tags, and which drivers support them?
- How do you implement HTTP response caching with ETag and Last-Modified headers in Laravel?
- What are the main production performance commands (route:cache, view:cache, config:cache)?
- What are the architectural risks of caching database query results, and how do you manage Invalidation?
- How does the Session Driver affect the speed and performance of a Laravel application?
- What is Redis Pipelining, and how does it reduce network latency for hundreds of commands in Laravel?
- What is Probabilistic Early Expiration, and how does it prevent a cache stampede?
- How do automatic Eloquent model caching packages (such as laravel-model-caching) work, and what are their downsides?
Testing & Quality Assurance Interview Questions (PHP & Laravel)
- What is the Pest PHP testing framework, and why has it become a preferred option for modern Laravel tests over PHPUnit?
- How do you write integrated Laravel Feature tests for API endpoints and validate JSON responses?
- How do the RefreshDatabase Trait and DatabaseTransactions differ in Laravel tests?
- How do Model Factories and Faker simplify preparing test data?
- How do you replace services and Facades with Mocks in Laravel tests without breaking the system?
- How do you control and freeze system time in Laravel tests that depend on timeouts?
- How do multi-channel Notifications work in Laravel, and how do you test them with Notification::fake()?
- How do you test Jobs, Events, and mail in Laravel with fakes (Queue::fake, Event::fake, and Mail::fake)?
- How do you test authentication and authorization (actingAs, Gates, and Policies) in Laravel applications?
- How do you test code that calls external APIs with Http::fake() and prevent real requests?