What does the "?" mean in code laravel like this?
$followers = $actionRequest->followers ? [
'instagram' => $actionRequest->followers?->instagram,
'tiktok' => $actionRequest->followers?->tiktok,
] : null;
What does the "?" mean in code laravel like this?
$followers = $actionRequest->followers ? [
'instagram' => $actionRequest->followers?->instagram,
'tiktok' => $actionRequest->followers?->tiktok,
] : null;
2
Answers
This is the ternary operator. It is a conditional operator just like "if".
Given that the first operand evaluates true, evaluate as second operand, else evaluate as third operand.
This means the code will load the followers into the variable $followers if $actionRequest->followers which might be a boolean is true but will be assigned null
Note:
?-> is null safe operator. Which is a different operator altogether
this is called ternary operation.
the value of
$x
isexpr2
ifcondition
is trueand
expr3
ifcondition
is false .so in your example the value of
$followers
if$actionRequest->followers
is true, is[ 'instagram' => $actionRequest->followers?->instagram, 'tiktok' => $actionRequest->followers?->tiktok, ]
, unless it return
null
.