The efficiency problem of ternary operator-condition (ternary) operator

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.