Home > Mobile >  isset() on the result of an expression
isset() on the result of an expression

Time:02-23

I have this isset() below because I want to show an empty string if the result of $this->field($content, 'description') is null.

However doing like this it shows

Cannot use isset() on the result of an expression

Do you know how to properly achieve this?

return [
    'description' => isset($this->field($content, 'description') ? $this->field($content, 'description') : '',
    ...
];

CodePudding user response:

I'd use the null coalescing operator ?? (https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce)

return [
    'description' => $this->field($content, 'description') ?? '',
    ...
];

CodePudding user response:

You can use php ternary ?: operator

return [
    'description' => $this->field($content, 'description') ?: '',
    ...
];
  •  Tags:  
  • php
  • Related