1. What does the nullsafe operator ( ?-> ) do in PHP 8?
The nullsafe operator prevents runtime errors when accessing methods or properties on null objects by returning null instead of throwing an error.
$user?->getProfile()?->getName();
Get the Preplance app for a seamless learning experience. Practice offline, get daily streaks, and stay ahead with real-time interview updates.
Get it on
Google Play
4.9/5 Rating on Store
PHP · Question Set
PHP 8+ Features interview questions for placements and exams.
Questions
15
Included in this set
Subject
PHP
Explore more sets
Difficulty
Mixed
Level of this set
Go through each question and its explanation. Use this set as a focused practice pack for PHP.
The nullsafe operator prevents runtime errors when accessing methods or properties on null objects by returning null instead of throwing an error.
$user?->getProfile()?->getName();
For complete preparation, combine this set with full subject-wise practice for PHP. You can also explore other subjects and sets from the links below.
The str_contains function returns true if one string is found within another. It replaces the need for using strpos with manual comparison.
str_contains('Hello World','World');Constructor property promotion simplifies class definitions by allowing properties to be defined and assigned directly in the constructor parameter list.
class User { function __construct(public string $name, private int $age){} }JIT, or Just in Time compilation, improves performance by compiling PHP bytecode into native machine code during execution, reducing interpretation overhead.
Union types let you declare that a parameter or return value can accept multiple types, such as int or float. It improves type safety and flexibility.
function sum(int|float $a, int|float $b){ return $a+$b; }The match expression compares a value against multiple conditions like switch, but it returns a value, supports strict comparison, and doesn’t require break statements.
echo match($status){ 200 => 'OK', 404 => 'Not Found', default => 'Error' };Named arguments allow passing arguments to functions by name instead of position, improving code readability and allowing flexible argument ordering.
displayUser(age:25, name:'John');
Attributes allow developers to add structured metadata to classes, methods, or properties. They can be read using reflection for configuration or annotations.
#[Route('/home')] class HomeController {}The mixed type allows a variable or function to accept any type, including int, float, string, array, object, or null. It’s useful for flexible APIs.
function processData(mixed $data){ return $data; }A WeakMap stores object references as keys without preventing them from being garbage collected, improving memory efficiency in complex systems.
$map = new WeakMap();
Match is an expression that returns a value, uses strict comparison by default, and doesn’t require break statements. It’s more concise and avoids fall-through issues common in switch statements.
Attributes add metadata to code structures. They are commonly used in frameworks for routing, validation, or ORM configuration, replacing PHPDoc comments with structured syntax that can be parsed at runtime.
It simplifies null checks by allowing chained property or method calls without explicit if statements. If any object in the chain is null, the entire expression safely returns null instead of causing an error.
JIT compilation turns frequently executed PHP bytecode into native machine code at runtime. This reduces CPU overhead, leading to faster execution for computation-heavy scripts like data processing or graphics generation.
PHP 8 introduced faster performance with JIT, better type safety using union and mixed types, cleaner syntax through constructor property promotion, and improved developer experience with named arguments and match expressions. It also enhanced error handling and security consistency.