never seen this syntax... explain please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

never seen this syntax... explain please

Post 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;
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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
Post Reply