skip to Main Content

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

Answers


  1. 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'] : '' ?>
    
    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
  5. 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');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search