Jump to content


Photo

What do '?' and ':' do?


  • Please log in to reply
2 replies to this topic

#1 HookedOnWeb

HookedOnWeb
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 14 March 2006 - 10:25 PM

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:
$summary
$conf['app']['prefixNameOnSummary']
$rs['fname']
$rs['lname']
$rs['summary']

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?


#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 14 March 2006 - 10:34 PM

As pulled from this (http://us3.php.net/m...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:

<?php
$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]

EDIT:

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]

#3 HookedOnWeb

HookedOnWeb
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 14 March 2006 - 10:43 PM

Thank you so much that really helped me out.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users