Jump to content


This topic is now archived and is closed to further replies.


What do '?' and ':' do?

Recommended Posts

Can someone please help me better understand this line of code?

$summary = ($conf['app']['prefixNameOnSummary']) ? "{$rs['fname']} {$rs['lname']}\n " . htmlspecialchars($rs['summary']) : htmlspecialchars($rs['summary']);

I understand these are variables:

I understand that \n is a new line.
I understand what the "." is doing.
I understand what htmlspecialchars is doing to $rs['summary'].

What I don't understand is this syntax:
$summary = () ? "prints the variables between the quotes" . variable : variable

What is the ? and : for and why is the variable before and after the colon?
I understand that the '.' joins the part between the quotes to the $variable : $variable

Can someone please tell me what this function is called so that I can look it up and understand how it works?

Share this post

Link to post
Share on other sites
As pulled from this (http://us3.php.net/manual/en/language.expressions.php) page in the manual:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--] There is one more expression that may seem odd if you haven't seen it in other languages, the ternary conditional operator:

$first ? $second : $third

If the value of the first subexpression is TRUE (non-zero), then the second subexpression is evaluated, and that is the result of the conditional expression. Otherwise, the third subexpression is evaluated, and that is the value.[/quote]


Also, see this page:

[a href=\"http://us3.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary\" target=\"_blank\"]http://us3.php.net/manual/en/language.oper...parison.ternary[/a]

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.