How do I print an array value as a template tag as blank in shorthand, even if it's null?

307 Views Asked by At

If I have this in my main php file (eg. my controller)

$debate['title'] = NULL;

And this in my template file (eg. my views file), where I can include PHP with my HTML. Imagine that I'm using a template engine or PHP as a templating engine.

<?=$debate['title'];?>

Note the = after the <? that makes it a shorthand way to include php variables and array keys in my template, to be shown on an HTML web page.

Well now in PHP 7.4 onwards, if $debate['title'] is null, I get this error. (That's if the notice severity level of errors is setup to be displayed on your screen.)

Message: Trying to access array offset on value of type null

I know that Stack Overflow would want me to use isset() but using something like

<?php if (isset($debate['title'])) { echo "$debate[title]"; } ?>

It just doesn't have the same ring to it. It's not really shorthand, is it?

5

There are 5 best solutions below

0
Lajos Arpad On BEST ANSWER

In general you can use the null coalescing operator, like

$debate['title'] ?? ''

If you pass this to a function, then you can apply the same inside the function parameters, like:

htmlspecialchars($debate['question'] ??  'array is blank');
3
Imran On

There is an operator in PHP(from version 7.0) called null coleascing operator which is denoted by two question marks(??). Null colescing operator can be helpful in your case. For example, you can simply write this code as below:

<?= $debate['title'] ?? '' ?>

which is equavalent to writing:

<?= isset($debate['title']) ? $debate['title'] : '' ?>
2
Ninja Work On

Please use ternary operator

when array value is null, it will print blank.

<?=$debate['title']= null;?>
<?= $debate['title'] ?: ''; ?>

Or if array value is not null, it will print the value
1
Luiz Renato Miranda On

In order not to change your template file, you could assign the empty value ('') to the variable in your controller ($debate['title'] = ''), or use the ternary operator to perform this check ($debate['title'] ?: '').

The displayed error just indicates that you are trying to access a NULL value.

0
Luis Zorreta On

You can use the "null coalescing" operator (??) to provide a default value if the array value is null. The "null coalescing" operator returns the value of its left operand if it exists and is not null, and the value of its right operand otherwise.

For example:

<?=$debate['title'] ?? '';?>

This will print an empty string if $debate['title'] is null, and will print the value of $debate['title'] if it exists and is not null.

Alternatively, you can use the "ternary operator" (?:) to achieve the same effect:

<?=$debate['title'] !== null ? $debate['title'] : '';?>

This will also print an empty string if $debate['title'] is null, and will print the value of $debate['title'] if it exists and is not null.

Both of these approaches are shorthand ways to provide a default value for an array value in a template.