22

What does ?: in this line mean?

$_COOKIE['user'] ?: getusername($_COOKIE['user']);

Thank you.

Lewis
  • 14,132
  • 12
  • 66
  • 87

4 Answers4

31

It is a shorthand for an if statement.

$username = $_COOKIE['user'] ?: getusername($_COOKIE['user']);

Is the same as

if( $_COOKIE['user'] ) 
{
    $username = $_COOKIE['user'];
} 
else
{
    $username = getusername($_COOKIE['user']); 
}

see test suite here: https://3v4l.org/6XMc4

But in this example, the function 'getusername' probably doesn't work correct, because it hits the else only when $_COOKIE['user'] is empty. So, the parameter inside getusername() is also kind of empty.

jko
  • 2,068
  • 13
  • 25
trizz
  • 1,447
  • 12
  • 27
  • if this line is `$username = $_COOKIE['user'] ?: getusername($_COOKIE['user']);`. Does that mean `if($_COOKIE['user']) { $username = $_COOKIE['user']; } else { $username = getusername($_COOKIE['user']); }`? – Lewis May 20 '14 at 07:07
  • I got it now. Thank you. – Lewis May 20 '14 at 07:13
  • 3
    Note with the ternary operator that your variable is going to be set to the output of your conditional. So while `$a = $b ?: $c;` will set `$a` to equal either `$b` or `$c` in all cases, `$a = ($b > 5) ?: $c;` then `$a` will only ever equal `true` or `$c` and never `$b`. It's a tricky nuance. – Tyler V. May 27 '16 at 02:06
  • Aw, I thought I could use it like `$var = isset($something) ?: $something` and I'd get `if (isset($result)) { $var = $result } else { $var = false; }`. Back to using `$var = isset($something) ? $something : null` for me... : – i336_ May 10 '19 at 11:53
  • FYI if you want to test if a variable, array key, object parameter, etc. exists (same as `isset()` or `empty()`) and if not assign another value you can use the `??` operator instead. For example: `$myVar = $varNotExists ?? 'this will be the value of $myVar because $varNotExists does not exist';` – Gavin Jun 01 '22 at 10:57
2

It is short hand php, for example:

(true == true ? echo "this is true" : "this is false")

Written out this means:

if (true == true) {
    echo "This is true";
}
else {
    echo "This is false";
}

In your example, there is only an else statement.

Arko Elsenaar
  • 1,689
  • 3
  • 18
  • 33
1

It's known as the ternary operator, similar to what's commonly called an inline if. For instance, the following two examples:

a) $genderString = $genderAbbreviation == "M" ? "Male" : "Female";

b)

if ($genderAbbreviation == "M")
{
    $genderString = "Male";
}
else
{
    $genderString = "Female";
}

Both of these will have the same effect. The statement before the question mark is evaluated to be either true or false, and then if true the statement before the colon is executed, and if false the statement after the colon is executed.

For more information you can check the section titled "Ternary Operator" on the following page of the PHP documentation:

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

Nick Coad
  • 3,623
  • 4
  • 30
  • 63
0

if $_COOKIE['user'] value is exist then NULL else getusername($_COOKIE['user'] will work

it's a ternary operator in php

Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44