user829814
user829814

Reputation: 221

PHP question mark

$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!

Upvotes: 21

Views: 39725

Answers (7)

Aleks G
Aleks G

Reputation: 57316

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.

Upvotes: 3

Webberman
Webberman

Reputation: 11

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

This is the same as the following:

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

Upvotes: -1

nickf
nickf

Reputation: 546065

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

Upvotes: 52

Patrick Desjardins
Patrick Desjardins

Reputation: 140833

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

Upvotes: 3

Ovais Khatri
Ovais Khatri

Reputation: 3211

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.

Upvotes: 4

robamaton
robamaton

Reputation: 690

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.

Upvotes: 0

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

It is the ternary operator: it means

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

Upvotes: 0

Related Questions