Page 1 of 1

never seen this syntax... explain please

Posted: Tue Nov 04, 2003 9:14 am
by bg
The testlogin function returns the name of the current user logged in, if no user is logged in i believe it returns null. Ive programmed in VB, C++, java, and now PHP and Ive never seen the syntax used in the second line below. If someone could explain to me what is going on there i would be very appreciative. Thanks,

Brian

Code: Select all

$login = testlogin();
$user = ($login == "") ? "" : $login;

Posted: Tue Nov 04, 2003 9:21 am
by twigletmac
The syntax uses the ternary operator basically

Code: Select all

$user = ($login == "") ? "" : $login;
is a shorter version of

Code: Select all

if ($login == '') {
    $user = '';
} else {
    $user = $login;
}
I'm not entirely sure what the point of the code is though, because it could be rewritten as:

Code: Select all

$user = $login;
Mac

Posted: Tue Nov 04, 2003 11:04 am
by JAM
twigletmac wrote: I'm not entirely sure what the point of the code is though, because it could be rewritten as:

Code: Select all

$user = $login;
Mac
Not entirely correct, depending on desired effect as $login could be NULL. Just thought it should be mentioned.

"" == NULL equals False
"" === NULL equals True


For readers interesting;
http://se.php.net/manual/en/language.ty ... ull.syntax