Forum Moderators: coopster

Message Too Old, No Replies

"? " and " : " operators?

What are they? (Not sure if they are operators)

         

genericnumber1

12:47 am on Oct 5, 2006 (gmt 0)

10+ Year Member



What do the "? " and " : " operators do as used in...

$_action = isset($_REQUEST['action'])? $_REQUEST['action'] : 'view';

I dug through PHP manuals, the documentation, trying to figure out how it works.... just impossible to search for "? " efficiently >.>

Theory!
"? " is simply a quick "if" in variable assignment

Still no idea what " : " is.

eelixduppy

12:53 am on Oct 5, 2006 (gmt 0)



It's called the Ternary Operator [us2.php.net].


The expression (expr1)? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

Hope this helps ;)

genericnumber1

12:54 am on Oct 5, 2006 (gmt 0)

10+ Year Member



yup, clear as glass. thanks!

FiRe

1:55 pm on Oct 6, 2006 (gmt 0)

10+ Year Member



$_action = isset($_REQUEST['action'])? $_REQUEST['action'] : 'view';

To make it easier to understand...

if (isset($_REQUEST['action'])) {
$_action = $_REQUEST['action'];
}
else {
$_action = 'view';
}