33

I've just uploaded some old PHP files to a new server and am getting parse errors (Unexpected ':') on shorthand ternary ops. eg:

$y = $x ?: "Some default";

php version is 5.2.16 The code is littered with these shorthand ?:, so before changing them all I thought I'd see if anyone knows anything about this as I've not used PHP for a while now.

kjones
  • 923
  • 1
  • 7
  • 25
Paul Kersey
  • 437
  • 1
  • 5
  • 10
  • 1
    "old PHP files to a new server"- I think you mean, "new PHP files to an old server"! (The PHP syntax used here is newer than the version of PHP installed on your (old) server.) – MrWhite Jan 19 '18 at 11:32
  • Why does everyone call this expression ternary operator? It only takes two arguments. Isn't this [Elvis operator](https://en.wikipedia.org/wiki/Elvis_operator)? – ya.teck Mar 16 '21 at 11:47

2 Answers2

70

This is only available since PHP 5.3

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.1

See this example for more context.

or a more useful but note in the comments: http://www.php.net/manual/en/control-structures.if.php#102060


1http://php.net/manual/en/language.operators.comparison.php

Sᴀᴍ Onᴇᴌᴀ
  • 7,491
  • 8
  • 27
  • 56
azat
  • 3,425
  • 1
  • 25
  • 30
  • Incorrect, I'm using PHP 5.2.x and it's working fine. See Nightwolf's answer below... – philm Jun 08 '11 at 12:18
  • 3
    Nope, it can`t be PHP 5.2.x. Maybe in your PHP version work this `$a = $foo ? 'foo' : 'bar'` ? – azat Jun 08 '11 at 12:22
  • 2
    The shorthand for PHP 5.2.x requires the empty value as in my answer below, but the above should work fine in PHP 5.3 . – Nightwolf Jun 08 '11 at 12:27
  • 1
    (For Googlers in a hurry: philm and Nightwolf were wrong, and the PHP manual was right. ;) Nightwolf eventually corrected himself at/below his answer.) – Sz. Feb 21 '18 at 12:00
  • 1
    I would like to add the following: if `expr1` evaluates to `true`, the *result* of that evaluation is returned. In other words, if `expr1` is true-ish, it will not be evaluated again, but the result of the expression will be used as when it was first evaluated. – XedinUnknown Jun 12 '18 at 15:39
0

Since you are using php 5.2.16, your ternary requires 2 options e.g

$y = $x? "???" : "Some default";

Variable = condition ? true value : false value ;

Nightwolf
  • 907
  • 2
  • 9
  • 22
  • PHP 5.2.x requires this syntax, but the original question's syntax works fine in 5.3. – Nightwolf Jun 08 '11 at 12:28
  • 8
    This is wrong. This is whether a *shorthand* ternary, nor is it equivalent of what the user wants. The equivalent in PHP 5.2 would be `$y = $x ? $x : 'default';`. – igorw Jun 09 '11 at 19:13
  • @igorw: I only told the user why he is getting the error in order for him to learn, not to blindly copy code. – Nightwolf Jun 10 '11 at 07:41
  • 4
    To re-iterate, the short-hand `?:` is only available in 5.3+ – John Carter Sep 25 '12 at 04:41
  • 2
    @Nightwolf that's not _shorthand_, that's the _full_ ternary operator. It seems you were getting confused because the ternary operator itself is a short version of if/else, but this question is about the shorthand ternary operator, and you're just talking about the standard ternary operator. Sorry to address such an old answer but it seems no one managed to clear up your confusion at the time. – Niall Jan 20 '14 at 12:27
  • @Niall When I wrote the answer I only knew and confirmed the requirements of the shortened "if/else" in that php version and believed it to be called shorthand. Changed to say ternary instead of shorthand. – Nightwolf Jan 21 '14 at 10:17