Subject: Need Help - Newbie
Posted By: Nickph Post Date: 9/23/2004 11:46:47 AM
Just started learning PHP...

looking at some code with statement:

$_SESSION[user][id] = $_POST[login_email] ? $_POST[login_email]: $_SESSION[user][id];

need to understand what this statement does
where can I find info on the operators ? :

Thanks , did not find info in my Beginning PHP4 book
Reply By: Snib Reply Date: 9/23/2004 12:34:45 PM
http://us4.php.net/manual/en/language.expressions.php

Basically, this is what this line is doing:

if($_POST[login_email])$_SESSION[user][id] = $_POST[login_email];

HTH,

-Snib <><
http://www.snibworks.com
There are only two stupid questions: the one you don't ask, and the one you ask more than once ;-)
Reply By: Nickph Reply Date: 9/24/2004 9:28:28 AM
Thanks
most helpful

Reply By: richard.york Reply Date: 9/26/2004 7:58:34 PM
That's called the ternary operator, it's an expression. You can find it in the PHP manual here:
http://www.php.net/manual/en/language.expressions.php

What happens is there are three subexpressions within the expression

$first? $second : $third;

The $first evaluates to a simple boolean. If it's true, the second subexpression is evaluated, which is the $second variable in the above example. If the $first subexpression evaluates to false, the third subexpression is evaluated.

Often times this can be represented in an identical if/else chain.

The ternary expression:
$foo = ($_POST['field'])? true : false;

is the same as this if/else chain:
if ($_POST['field'])
{
    $foo = true;
}
else
{
    $foo = false;
}

HTH!

Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail

Go to topic 19594

Return to index page 763
Return to index page 762
Return to index page 761
Return to index page 760
Return to index page 759
Return to index page 758
Return to index page 757
Return to index page 756
Return to index page 755
Return to index page 754