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? 🙂
This is called a ternary operator. It is presented in this form :
For example you can use the result of this expression for an assignment, let’s say :
In fact, this is the equivalent of writing :
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 :
In the classic IF/ELSE form, you would write :
But now, watch the beauty of ternary operator :
You can read all about the ternary operator here:
php.net manual – ternary operator
it is the conditional operator in php, also called ternary operator. Because it have 3 operands.
Actually it is a short version of if-else statement. Find an example on the following link
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.