26. What are the major differences between PHP 7 and PHP 8?
Answer:
PHP 8 introduced:
- JIT (Just-In-Time) compiler
- Union types
- Named arguments
- Attributes (annotations)
- Match expression
- Improved performance and type safety
PHP 8 is significantly faster and more modern in syntax.
27. What is Composer?
Answer:
Composer is a dependency manager for PHP. It manages libraries and packages defined in composer.json, and follows PSR standards for autoloading.
28. What is PSR?
Answer:
PSR (PHP Standards Recommendation) defines coding standards and interoperability rules (e.g., PSR-4 for autoloading). It ensures consistency across projects.
29. Explain MVC architecture in PHP.
Answer:
- Model: Handles data and business logic
- View: Presentation layer
- Controller: Handles requests and coordinates model/view
It separates concerns and improves maintainability.
30. What frameworks have you used in PHP?
Strong Answer Example:
“I’ve worked with Laravel, which provides MVC, Eloquent ORM, middleware, routing, authentication scaffolding, and built-in security protections.”
(If you haven’t used one, mention understanding of Laravel/Symfony structure.)
31. Explain dependency injection in PHP.
Answer:
Dependencies are injected via constructors or service containers rather than instantiated inside classes. Frameworks like Laravel use service containers for DI.
32. What are traits in PHP?
Answer:
Traits allow horizontal code reuse. They let you include methods in multiple classes without inheritance.
33. How does PHP handle sessions?
Answer:
PHP stores session data on the server and tracks users via a session ID stored in a cookie.
34. What is output buffering?
Answer:
Output buffering stores script output in memory before sending it to the browser. It allows modification of headers before output.
35. What is the difference between include and require?
Answer:include gives a warning if file missing.require throws fatal error if file missing.
36. How do you prevent SQL injection in PHP?
Answer:
- Use prepared statements (PDO)
- Validate and sanitize input
- Use parameterized queries
37. What is PDO?
Answer:
PHP Data Objects (PDO) is a database abstraction layer that supports multiple databases and prepared statements.
38. PDO vs MySQLi?
Answer:
PDO supports multiple databases.
MySQLi is MySQL-specific.
PDO is preferred for flexibility.
39. What is an ORM in PHP?
Answer:
ORM maps database tables to objects. Example: Eloquent (Laravel).
40. How do you structure a large PHP application?
Answer:
- MVC architecture
- Service layer
- Repository pattern
- PSR-4 autoloading
- Environment configs
- Dependency injection
41. What are namespaces in PHP?
Answer:
Namespaces prevent class name conflicts and organize code logically.
42. What is autoloading?
Answer:
Autoloading automatically loads class files when needed, typically using Composer with PSR-4.
43. What is RESTful API development in PHP?
Answer:
Building APIs using HTTP methods:
- GET
- POST
- PUT
- DELETE
Returning JSON responses and proper status codes.
44. How do you handle authentication in PHP?
Answer:
- Session-based authentication
- Token-based (JWT)
- OAuth implementations
- Hash passwords using
password_hash()
45. How would you secure a PHP application?
Answer:
- Use HTTPS
- Prepared statements
- Input validation
- CSRF protection
- XSS prevention
- Proper file permissions
- Secure headers