I understand basically what it is saying, to create a session variable for ‘name’ if is is already set, and if it is not already set then let it be equal to null. But what exactly does the ? and the : do in this code?
It can also be used for more than two options by adding another ternary operator as the ELSE of the first. Say you have the option of showing First Name, User Name or No Name. More than likely these variables WOULD be set from a DB query and thus I would use !empty() but I’m using isset() in this example.
For those coming to PHP from another language, a very important heads up about PHP’s ternary operator - it’s left associative and as a result broken and can’t be chained!!
if (isset($_SESSION['name']){ // The expression before the question mark
// between the question mark and the colons
$_SESSION['name'] = $_SESSION['name'];
} else {
// after the colons
$_SESSION['name'] = $_SESSION['name'];
}