Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to safely access the property without error if the object is null.
PHP
$length = $obj[1]->length; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '->' causes an error if the object is null.
Using '::' is for static access, not for object properties.
Using '->?' is not valid syntax.
✗ Incorrect
The null safe operator in PHP is '?->' which allows safe access to properties or methods even if the object is null.
2fill in blank
mediumComplete the code to safely call the method if the object is not null.
PHP
$result = $user[1]->getName(); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '->' causes fatal error if $user is null.
Using '::' is for static methods, not instance methods.
Using '->?' is invalid syntax.
✗ Incorrect
Using '?->' calls the method only if $user is not null, otherwise returns null safely.
3fill in blank
hardFix the error in safely accessing the nested property.
PHP
$city = $person[1]->address[2]->city;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '->' causes errors if any intermediate property is null.
Using '::' is incorrect for instance property access.
Using '->?' is invalid syntax.
✗ Incorrect
Use '?->' for both property accesses to safely handle null values in the chain.
4fill in blank
hardFill both blanks to safely access the nested method call.
PHP
$result = $order[1]->getCustomer()[2]->getName();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '->' causes errors if any method returns null.
Using '::' is for static methods only.
Using '->?' is invalid syntax.
✗ Incorrect
Both method calls use '?->' to safely handle null values in the chain.
5fill in blank
hardFill all three blanks to safely access a deeply nested property.
PHP
$value = $config[1]->settings[2]->display[3]->theme;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '->' causes fatal errors if any property is null.
Using '::' is invalid for instance properties.
Using '->?' is not valid syntax.
✗ Incorrect
All property accesses use the null safe operator '?->' to avoid errors if any part is null.