Jump to content

What do '?' and ':' do?


HookedOnWeb

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:
$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?
Link to comment
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:

[code]<?php
$first ? $second : $third
?>[/code]

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.