Skip to content

Chapter 36 of 38

Modern PHP Syntax

Apply coalescing, arrows, nullsafe access, named arguments, and promotion.

34 minutes 10 quick checksBy Subha Prasad
Lesson 36 of 38Course navigation

Lesson content

Read, practise, then check your understanding

PHP 7/8 added concise syntax that can improve intent when not overcompressed.

Practical example

<?php
$name = $input['name'] ?? 'Anonymous';
$upper = array_map(fn(string $value): string => strtoupper($value), $names);
$country = $user?->address?->country;
$service = new Service(logger: $logger, timeout: 5);

Arrow functions capture outer values by value. Nullsafe access yields null but can hide missing required state, so use it only for genuinely optional chains. Named arguments couple callers to parameter names. Promotion works best with validation and readonly dependencies.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes null coalescing?
Which PHP term matches this description: A concise fallback for unset or null values.
Which statement best describes arrow function?
Which PHP term matches this description: A concise fn expression capturing outer variables by value.
Which statement best describes nullsafe operator?
Which PHP term matches this description: The ?-> operator stopping a member chain on null.
Which statement best describes named argument?
Which PHP term matches this description: A call argument supplied by parameter name.
Which statement best describes constructor promotion?
Which PHP term matches this description: Parameter syntax declaring and initializing properties.

0 of 10 checks passed

Your progress is saved on this device.