1

I am going through a few WordPress tutorials and I have no idea what is going on here, I've found that a question mark can be representative of an if statement, however I really haven't a clue what the following line is actually doing.

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

Anyone care to help me out? :)

dingo_d
  • 11,160
  • 11
  • 73
  • 132
Peter Turner
  • 335
  • 1
  • 4
  • 17
  • 1
    It's called a ternary, you can read more here.. http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary – Dale May 20 '16 at 10:58
  • 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) – YvesLeBorg May 20 '16 at 11:02
  • As others have said really. It is like a short hand if / else on one line of code. It is a valid syntax in other languages too, like C++. – Andrew Truckle May 20 '16 at 11:54

4 Answers4

4

This is called a ternary operator. It is presented in this form :

condition ? value_if_condition_true : value_if_condition_false;

For example you can use the result of this expression for an assignment, let's say :

$load_page = is_user_logged_in() ? true : false;

In fact, this is the equivalent of writing :

 if (is_user_logged_in())
     $load_page = true;
 else
     $load_page = false;

EDIT Because I love the ternary operator, I want to write more. This operator form has no effect on performance or behaviour compared to the classic else/if format, and only serves the purpose of being an elegant one liner. (Which is the reason why some languages don't implement it, taking the view that it is unnecessary and often bad practice to have N ways of writing the same instruction)

But to show how elegant it is, imagine you know the age of a user, and the age_of_subscription to your website, and you want to display a message depending on these two variables according to these rules :

  • if the user is older than 20 and had a subscription for more than 3 year, he is allowed to a discount
  • if the user is younger than 20 and had a subscription for more than 1 years, he is allowed to a discount
  • in any other case, the user is paying the full price.

In the classic IF/ELSE form, you would write :

if ($age > 20)
{
    $text = "You are above 20 and you have been a user for ";
    if ($age_of_subscription > 3)
    {
        $text .= "more than 3 years so you are allowed a discount";
    }
    else
    {
        $text .= "less than 3 years so you are paying full price";
    }

}
else
{
    $text = "You are below or 20 and you have been a user for ";
    if ($age_of_subscription > 1)
    {
        $text .= "more than 1 year so you are allowed a discount";
    }
    else
    {
        $text .= "less than 1 year so you are paying full price";
    }


}
echo $text;

But now, watch the beauty of ternary operator :

echo 'You are ',
($age > 20 ? 
    ($age_of_subscription > 3 ? 'above 20 and you have been a user for more than 3 years
\ so you are allowed a discount' : 'above 20 and you have been a user for less than 3 years
\ so you are paying full price.')
    : ($age_of_subscription > 1 ? 'below or 20 and you have been a user for more than 1 year
\ so you are allowed a discount' : 'below or 20 and you have been a user for less than 1 year
\ so you are paying full price.')); // pretty uh?
zoubida13
  • 1,738
  • 12
  • 20
0

it is the conditional operator in php, also called ternary operator. Because it have 3 operands.

 (expression) ? value to be returned if expression is true : value to be returned if expression is false;
Actually it is a short version of if-else statement. Find an example on the following link
[http://www.tutorialspoint.com/php/php_conditional_operator_examples.htm][1]
Shobi
  • 10,374
  • 6
  • 46
  • 82
  • "do this part if true:else do this part" this is not the best wording, the second and third operands are not actions but return values for the overall expression, which could indeed be functions. – zoubida13 May 20 '16 at 11:05
  • 1
    Thanks @zoubida13. answer is edited. This is a new account, so icoudn't post a comment thats why i did it in the answer – Shobi May 21 '16 at 07:09
0

You can read all about the ternary operator here:

php.net manual - ternary operator

-1

The following code snippet

S?T:F;

is a short evaluation method for if else condition. 'S' relates to conditional statement, 'T' is the statement which is to be executed if statement is true.

If 'S' is false, 'F' is executed.

bmonikraj
  • 52
  • 4