3

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

Mohammad
  • 21,175
  • 15
  • 55
  • 84
Merrick
  • 237
  • 3
  • 11
  • The error says, that `T_UNSET` (that is the token for `unset`) is unexpected at that line. That means you can not place it there. That's all. Remove it and you're fine. This has not much to do with the ternary operator btw.. – hakre May 07 '12 at 01:38

4 Answers4

6

You can't use unset inside of the ternary operation because it is not an expression that can be operated on. It's a language construct (like isset and echo) and can't be placed there.

Just use it and you're fine, no decision is needed:

unset($items['blog']);
Paul
  • 139,544
  • 27
  • 275
  • 264
Bryan
  • 6,682
  • 2
  • 17
  • 21
  • 2
    An additional note, you actually don't even need the if statement. unset($items['blog']) won't return an error if the key doesn't exist, that's probably your best bet :) – Bryan May 07 '12 at 01:18
  • 3
    This isn't true. You can run a function inside the ternary operator: http://codepad.org/X5bDeXwb – Paul May 07 '12 at 01:20
  • exactly, unset is not a function, it's an operator. – Elias Dorneles May 07 '12 at 01:22
3

The error says, that T_UNSET (that is the tokenDocs for unset) is unexpected at that line. That means you can not place it there. That's all. Remove it and you're fine:

unset($items['blog']);

This has not much to do with the ternary operator btw., and as the code example shows, you don't need that operator for unset anyway.

If you love ternary operators very much, you can eval the unset:

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

but that's not a serious suggestion because not very straight-forward.

hakre
  • 193,403
  • 52
  • 435
  • 836
1

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.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • there is no problem with function calls in ternary operators. but unset is no function but a language construct. and that's why it does not work. However your suggestion is still the best suggestion here code-wise, just not the right explanation. – hakre May 07 '12 at 01:22
  • @hakre I know you edited the other post but I can't @ you on it since you haven't commented there. Just so you're aware, `if` and `for` are not language constructs. The full list of constructs is the third category of reserved words on this page: http://php.net/manual/en/reserved.keywords.php – Paul May 07 '12 at 01:31
  • @PaulP.R.O.: I have a more relaxed view on language constructs here, and I think the example with `if` or `for` are better. Because: `isset` does work: http://codepad.org/MCSHpbTG – hakre May 07 '12 at 01:35
  • @hakre That is very interesting. I'm surpised that isset doesn't cause another error. Perhaps it's because it's a language construct that returns a boolean, in case you want to nest ternary isset tests. – Paul May 07 '12 at 01:42
  • @PaulP.R.O.: See http://www.php.net/manual/en/tokens.php which explains the error message. – hakre May 07 '12 at 01:44
  • 2
    @hakre That doesn't explain why you can't use it in a ternary operator though, but can use isset. I just discovered this: http://ca2.php.net/manual/en/language.expressions.php which explains it. An expression is "anything which has a value". `unset` just like `echo` has no return value. So they cannot be used there since an expression is expected. Any function can be used there and some language constructs which return values can also be used. – Paul May 07 '12 at 01:49
  • Although die and exit can still be used oddly enough, even though they are not expressions. – Paul May 07 '12 at 01:57
  • @PaulP.R.O.: No, otherwise `die` won't work, but it does: http://codepad.org/wmd66UC8 - it's just a parser thing, PHP tells you syntax error, accept it. You can not argue with the information given in the manual, if you want to explain this technically, take a look into the PHP source-code and see where those parser errors are created. - Edit: Ah now I see you found die as well. – hakre May 07 '12 at 02:01
0

just a suggest regarding to benchmark matters : only use ternary syntax when you have to e.g in a view or if you really need one line code; therefore if else operator is much faster

Marcel Djaman
  • 1,276
  • 1
  • 17
  • 34
  • Interesting; have you tested this or just noticed the difference? This is also too bad, not that a ternary is easier to read than an if/else, but I love the convenience. – Merrick May 07 '12 at 02:32