Conditional (ternary) operator (?:)
The conditional operator?: accepts three operands, which is the only ternary operator in C #.
Return one of the following two expressions, as appropriate.
Test? Expression1:expression2
The expression expression1 returned when test is true. may be a comma expression.
The expression expression2 returned when test is false. may be a comma expression.
?: operator can be used as a shortcut to a if...else statement.
Conditional operator (ternary operator) efficiency problem
Let's take a look at this piece of code:
$name = GET('name') != null ? GET('name') : '';
function GET($key)
{
if (isset($_GET[$key]))
{
$fp = fopen('c.txt','a');
fwrite($fp, '1-');
fclose($fp);
return $_GET[$key];
}
return null;
}
After running, found that the contents of the C.txt file is 1-1-
Obviously, the GET function was executed two times.
Later still in this form:
$name = GET('name');
$name = $name != null ? $name : '';
Although the code is a bit long, the efficiency is relatively small (if the call is a memory-intensive function, the problem is obvious)
Operator Precedence