A three-object operator problem

Source: Internet
Author: User
{Code...} does not understand the result. why is it & quot; excessive & quot ;? Q &
$a = 5;$b = 10;echo ($a) ? ($a == 5) ? 'yay' : 'nay' : ($b == 10) ? 'excessive' : ':('; 

Why is it "excessive "? Q &

Reply content:
$a = 5;$b = 10;echo ($a) ? ($a == 5) ? 'yay' : 'nay' : ($b == 10) ? 'excessive' : ':('; 

Why is it "excessive "? Q &

Modify the value and analyze it based on the result.

//#1 excessive$a = 5;$b = 10;echo ($a) ? ($a == 5) ? 1 : 0 : ($b == 10) ? 'excessive' : ':('; //#2 :($a = 3;$b = 10;echo ($a) ? ($a == 5) ? 1 : 0 : ($b == 10) ? 'excessive' : ':('; //#3 excessive$a = 0;$b = 10;echo ($a) ? ($a == 5) ? 1 : 0 : ($b == 10) ? 'excessive' : ':('; //#4 :($a = 0;$b = 9;echo ($a) ? ($a == 5) ? 1 : 0 : ($b == 10) ? 'excessive' : ':('; 

It can be seen that $ a is executed when it is true ($ a = 5 )? 1: When 0 is false, run $ B = 10.
If this result is true, excessive is returned. if it is false, return :(

In order to facilitate reading, it is best to ($ a = 5 )? 1: 0. add parentheses.

I can only say that js runs out.yay

Is PHP executed like this?

($a ? ($a == 5 ? "yay" : "nay") : $b == 10) ? "excessive" : ":(";

It would be easier to write code by yourself or add a bracket group. for example, the following should be the same result as javascript.

echo $a    ? ($a == 5 ? "yay" : "nay")     : ($b == 10 ? "excessive" : ":(");

If I guess it is correct, I should look at it later and divide the statement.

The division can be simplified
Echo 'yay '? 'Excessive ':':(';


  

In this way, the result will change.

$ A = 5: yay is returned.

($ )? ($ A = 5 )? 'Yay': 'nay': ($ B = 10 )? 'Excessive ':' :( '; you read the statement from left to right, but the machine does not read it like this ($ )? ($ A = 5 )? 'Yay': 'nay': ($ B = 10 )? 'Excessive '):' ('; the computer head is dizzy.

$? ($ A = 5? 'Yay': 'nay'): ($ B = 10? 'Excessive ':' ('); will it be written like this later?

In terms of language design, js re-outputs, while php re-computing.
Similar differences include the calculation result of the & operator and | operator, and the result after null is converted into a string.

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.