21
$hideCode = $likesObj->isAlreadyLikedByUser(facebookUID()) ? 'style="display:none;"' : '';

Can anyone explain to me what that question mark does in this line of code? Thanks a lot!

Thomas Orlita
  • 1,554
  • 14
  • 28
user829814
  • 221
  • 1
  • 2
  • 4
  • http://www.php.net/manual/language.operators.comparison.php#language.operators.comparison.ternary – nmc Jul 05 '11 at 13:50
  • 2
    possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – John Conde Jul 05 '11 at 13:51

7 Answers7

52

This is called the Ternary Operator, and it's common to several languages, including PHP, Javascript, Python, Ruby...

$x = $condition ? $trueVal : $falseVal;

// same as:

if ($condition) {
    $x = $trueVal;
} else {
    $x = $falseVal;
}

One very important point to note, when using a ternary in PHP is this:

Note: Please note that the ternary operator is a statement, and that it doesn't evaluate to a variable, but to the result of a statement. This is important to know if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued in later PHP versions. source

nickf
  • 537,072
  • 198
  • 649
  • 721
  • ?: "There's also a shorthand version of this (in PHP 5.3 onwards). You can leave out the middle operand. The operator will evaluate as the first operand if it true, and the third operand otherwise." http://stackoverflow.com/questions/1276909/php-syntax-question-what-does-the-question-mark-and-colon-mean#1276912 – François Breton Oct 07 '16 at 17:44
4

Actually this statment is representing a Ternary operation, a conditional expression:

// works like:    (condition) ? if-true : if-false;

$hideCode = $likesObj->isAlreadyLikedByUser(facebookUID()) ?  'style="display:none;"':'';

in your case $hideCode will have style="display:none;" value if

$likesObj->isAlreadyLikedByUser(facebookUID())

will return true, else it will be null or empty.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
Ovais Khatri
  • 3,201
  • 16
  • 14
3

It's a shorter version of a IF statement.

$hideCode = $likesObj->isAlreadyLikedByUser(facebookUID()) ? ' style="display:none;"':'';

if in fact :

if($likesObj->isAlreadyLikedByUser(facebookUID()))
{
   $hideCode = 'style="display:none"';
}
else
{
 $hideCode = "";
}

For the purism :

It's representing a Ternary operation

Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
3

This is the simple if-then-else type logic:

(condition) ? (if-true-value) : (if-false-value)

so in your case, the condition is checked (i.e. has the page already been liked by the user); if yes (true condition), then style="display:none;" is printed so that whatever the element you're using is not displayed. Otherwise, an empty string is printed, which is the equivalent of not printing anything at all, naturally.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
0

It is the ternary operator: it means

if $likesObj->isAlreadyLikedByUser(facebookUID()) is true assign style="display:none; to the variable, otherwise assign ''

Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
0

It's part of a ternary operator. The first part is the conditional of an if-else statement. After the question mark is the "if" block, and after the colon is the "else" block.

robamaton
  • 690
  • 1
  • 6
  • 17
-1

$hideCode = $likesObj->isAlreadyLikedByUser(facebookUID()) ? 'style="display:none;"':'';

This is the same as the following:

if ($likesObj->isAlreadyLikedByUser(facebookUID()))
{
    $hideCode = 'style="display:none;"';
}
else
{
    $hideCode = '';
}
Webberman
  • 11
  • 5