Skip to content

PHP Tutorial PHP Interview and Practice

Modern PHP Interview Questions

Lesson 366 of 369 3 min read By Deepak Paswan Updated Oct 5, 2026

Q1. What are the main PHP 8.0 features?

Named arguments, union types, match, the nullsafe operator ?->, constructor property promotion, attributes, mixed, str_contains() and friends, throw as an expression, and the JIT compiler.

Q2. What is match and how does it differ from switch?

match is an expression that returns a value, uses strict comparison, needs no break, has no fall-through, and throws UnhandledMatchError if nothing matches and there is no default.

index.php
PHP
<?php
$code = 404;

echo match (true) {
    $code >= 500 => "server error",
    $code >= 400 => "client error",
    default => "ok",
};
Output
OUTPUT
client error

Q3. What is the nullsafe operator?

$a?->b?->c returns null as soon as something in the chain is null, instead of throwing an error.

Q4. What are enums?

A type with a fixed set of cases (PHP 8.1). Backed enums have a string or int value, with from() and tryFrom(). They can have methods, constants and interfaces.

index.php
PHP
<?php
enum Level: int
{
    case Low = 1;
    case High = 3;
}

echo Level::tryFrom(3)?->name . " " . var_export(Level::tryFrom(2), true);
Output
OUTPUT
High NULL

Q5. What is readonly?

A property modifier (PHP 8.1) that allows one assignment and then forbids changes. readonly class (PHP 8.2) makes every property readonly.

Q6. What are attributes?

Structured metadata on classes, methods, properties and parameters, written #[Name(args)], read with Reflection. Replace docblock annotations. Frameworks use them for routes and validation.

Q7. What are first-class callables?

strlen(...), $obj->method(...) and Class::method(...) create a Closure from a function or method (PHP 8.1).

Q8. What is a generator?

A function with yield that produces values one at a time and keeps its state between them. It saves memory when working with large data.

index.php
PHP
<?php
function evens(int $max): Generator
{
    for ($i = 2; $i <= $max; $i += 2) {
        yield $i;
    }
}

echo implode(",", iterator_to_array(evens(10)));
Output
OUTPUT
2,4,6,8,10

Q9. What are union and intersection types?

int|string accepts either. A&B requires both (PHP 8.1). DNF types (A&B)|null combine them (PHP 8.2).

Q10. What is Composer and what does PSR-4 do?

Composer is the dependency manager. composer.json lists requirements, composer.lock records exact versions, and vendor/autoload.php autoloads classes. PSR-4 maps a namespace prefix to a folder, so a class name maps to a file.

Q11. What are PSR-12 and strict types?

PSR-12 is the coding style standard. declare(strict_types=1) makes calls in that file type-strict.

Q12. What are the named arguments’ benefits and risk?

They make calls clearer and let you skip optional parameters. The risk: parameter names become part of a public API, so renaming one can break callers.

Q13. Which features came in PHP 8.4?

Property hooks, asymmetric visibility (public private(set)), new Foo()->method() without extra parentheses, array_find(), array_any(), array_all(), and the #[\Deprecated] attribute.

Q14. What came in PHP 8.5?

The pipe operator |>, array_first() and array_last(), clone with new values, the #[\NoDiscard] attribute and the URI extension.

Q15. How do you decide which PHP version to target?

A version that still receives security fixes (see php.net/supported-versions.php), and that your host supports. Use composer.json require.php to declare it.

Q16. What is an anonymous class?

A class without a name, created with new class { ... }, useful for one-off objects such as test doubles.