Search code examples
phpternary-operatorunset

Why unset() doesn't work in PHP ternary operator


So there is a problem with this, but i'm blind to it. Even after reading the documentation twice (PHP Comparison Operators)

isset($items['blog']) ? unset($items['blog']) : NULL;

Parse error: syntax error, unexpected T_UNSET


Solution

  • A @Bryan points out, no function calls to language constructs within the ternary operator. Since there's no return value involved at all here though, just do:

    unset($items['blog']);
    

    There's no need to check if the value is set or not beforehand. If it is not, unset simply won't do anything.