All interview guides

PHP Interview Questions and Answers

24 questions that come up in PHP technical interviews, each with the answer and an explanation of why it is right.

Test yourself — 90 question bank

1. What are generators?

Advanced

Answer: Functions using yield for memory-efficient iteration

Generators use yield instead of return. Create iterators without storing all values in memory. Memory efficient for large datasets. State preserved between yields.

2. What is PHP?

Beginner

Answer: Server-side scripting language for web development

PHP (Hypertext Preprocessor) is a popular general-purpose server-side scripting language especially suited for web development. Created by Rasmus Lerdorf.

3. What is Object-Oriented Programming in PHP?

Intermediate

Answer: Programming paradigm using classes and objects

OOP organizes code into classes (blueprints) and objects (instances). Supports encapsulation, inheritance, polymorphism. Modern PHP is heavily object-oriented.

4. How do you start and end a PHP code block?

Beginner

Answer: <?php and ?>

PHP code blocks start with <?php and end with ?>. Short tags <? ?> exist but are discouraged. Always use <?php for compatibility.

5. What is type hinting?

Advanced

Answer: Declares parameter and return types

Type hints specify parameter/return types: function add(int $a, int $b): int. Enforces types. PHP 7+ has scalar types, return types. Strict mode with declare(strict_types=1).

6. How do you define a class?

Intermediate

Answer: class ClassName { properties and methods }

Define class with class keyword: class User { public $name; public function greet() {} }. Convention: PascalCase for class names.

7. What is a constructor?

Intermediate

Answer: __construct() method called when object created

__construct() is magic method called automatically when creating object. Initialize properties: function __construct($name) { $this->name = $name; }. PHP 8 has constructor property promotion.

8. How do you output text in PHP?

Beginner

Answer: echo or print

Use echo or print to output text. echo can take multiple parameters, print returns 1. Both work, echo is more common and slightly faster.

9. What is strict typing?

Advanced

Answer: declare(strict_types=1) enforces strict type checking

declare(strict_types=1) at file start enforces strict types. No automatic type coercion. Must be first statement. File-level, not global. Recommended for type safety.

10. What are anonymous functions/closures?

Advanced

Answer: Functions without names, can capture variables

Anonymous functions: function($x) { return $x * 2; }. Closures capture variables with use: function() use ($var) {}. First-class functions. Used for callbacks.

11. How do you declare a variable in PHP?

Beginner

Answer: $variableName = value

Variables start with $ sign: $name = "John". PHP is loosely typed. Variable names are case-sensitive. Can contain letters, numbers, underscores.

12. What are visibility modifiers?

Intermediate

Answer: public, protected, private control access

public: accessible everywhere. protected: class and subclasses. private: only within class. Default is public. Essential for encapsulation.

13. What is inheritance?

Intermediate

Answer: Class extends another with extends keyword

Inheritance: class Child extends Parent. Child inherits parent's accessible properties/methods. Override with same method name. Use parent:: to call parent method.

14. What are the main data types in PHP?

Beginner

Answer: String, Integer, Float, Boolean, Array, Object, NULL

PHP supports: String, Integer, Float, Boolean, Array, Object, NULL, and Resource. Use gettype() to check type. Type juggling happens automatically.

15. What is late static binding?

Advanced

Answer: static:: refers to called class, not defined class

static:: (not self::) refers to called class at runtime. Enables proper inheritance of static methods. Essential for factory patterns. Resolved at runtime.

16. What is an interface?

Intermediate

Answer: Contract defining methods class must implement

Interface defines method signatures without implementation. Classes implement with implements keyword. Can implement multiple interfaces. All methods are public.

17. How do you concatenate strings?

Beginner

Answer: Use . operator: $a . $b

Concatenate strings with . operator: $full = $first . " " . $last. Use .= for append. String interpolation: "$first $last" in double quotes.

18. What are variadic functions?

Advanced

Answer: Functions accepting variable number of arguments with ...

Variadic functions use ...$args to accept variable arguments. function sum(...$numbers). ... also unpacks arrays. PHP 5.6+ feature for flexible functions.

19. What is an abstract class?

Intermediate

Answer: Cannot be instantiated, may have abstract methods

Abstract class cannot be instantiated. May have abstract methods (no implementation) and concrete methods. Child classes must implement abstract methods.

20. What is the difference between single and double quotes?

Beginner

Answer: Double quotes parse variables, single quotes literal

Double quotes parse variables and escape sequences. Single quotes treat everything literally. "Hello $name" vs 'Hello $name'. Single quotes slightly faster.

21. What is dependency injection?

Advanced

Answer: Passes dependencies instead of creating them

Dependency injection passes dependencies to class instead of creating internally. Constructor/setter/interface injection. Enables testing, loose coupling. Core of modern frameworks.

22. What is a service container?

Advanced

Answer: Manages class dependencies and injection

Service container manages dependencies. Registers services, resolves dependencies automatically. Central to Laravel, Symfony. Enables dependency injection at scale.

23. How do you create an array?

Beginner

Answer: array() or []

Create arrays with array() or [] (short syntax): $arr = [1, 2, 3]. Supports indexed and associative arrays. Dynamic sizing.

24. What is a trait?

Intermediate

Answer: Reusable code for horizontal code reuse

Traits enable code reuse in single inheritance. use TraitName in class. Can use multiple traits. Solves diamond problem. Methods can be aliased or excluded.

Ready to test yourself?

The full PHP bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.

Take the PHP quiz