php - Meaning of question mark colon operator

operator-keyword, php

Solution

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.

Problem

What does `?:` in this line mean? ``` $_COOKIE['user'] ?: getusername($_COOKIE['user']); ``` Thank you.

Original source

Related problems