What does the question mark as well as the colon (:) do in php, particularly in this line?

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? 🙂

Related posts

Leave a Reply

4 comments

  1. 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?
    
  2. 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]
    
  3. 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.