-1

can you guys help me?

I have these codes here,

$sUsername = isset($_SESSION['username']) ? $_SESSION['username'] : NULL;

what I want to ask is what does the ? $_SESSION['username'] : NULL; mean?

does it mean that it checks whether the session is null or not? I'm sorry but this is my first time seeing these codes so I'd really appreciate if someone can explain it to me or give me reference sites about this.

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Indra
  • 13
  • 5

2 Answers2

1

Just for you:

$sUsername = isset($_SESSION['username']) ? $_SESSION['username'] : NULL;

means

if (isset($_SESSION['username'])) {
    $sUsername = $_SESSION['username'];
}
else {
    $sUsername = NULL;
}

Check this: How do I use shorthand if / else?

Community
  • 1
  • 1
csklrnd
  • 184
  • 8
1

"?:" is known as the "Ternary Operator" ... it's shorthand for if/else

$sUsername = isset( $_SESSION['username'] ) ? $_SESSION['username'] : NULL ;

is equivalent to:

if( isset($_SESSION['username']) ){
    $sUsername = $_SESSION['username'];
} else {
    $sUsername = NULL;
}
Ragdata
  • 1,156
  • 7
  • 16