0
0
PHPprogramming~10 mins

Null safe operator in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A->
B->?
C::
D?->
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.
2fill in blank
medium

Complete 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'
A->
B::
C?->
D->?
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.
3fill in blank
hard

Fix 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'
A?->
B->
C::
D->?
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.
4fill in blank
hard

Fill 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'
A?->
B->
C::
D->?
Attempts:
3 left
💡 Hint
Common Mistakes
Using '->' causes errors if any method returns null.
Using '::' is for static methods only.
Using '->?' is invalid syntax.
5fill in blank
hard

Fill 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'
A?->
B->
C::
D->?
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.