Home  >  Article  >  Backend Development  >  求好手解析个php题目

求好手解析个php题目

WBOY
WBOYOriginal
2016-06-13 11:16:02889browse

求高手解析个php题目

<br /><?php<br />$arr = array(0 =>1,'aa' => 2,3,4);<br />    foreach($arr as $key => $val){<br />		echo "key:".$key.",jisuan:";<br />        print($key == 'aa' ? 5 : $val);<br />		echo "<br>";<br />    }<br /><br />?><br /><br />

答案为啥是:5534呢
我是这样分析的
1.因为有2个索引,所以循环2次(正确答案是循环了4次,为啥呢)
2.以为第一次的索引时0,不等于aa,所以值是1,然而正确答案是5,为啥呢
3.第二次索引是aa正好等于aa,所以是5,这个推测正确
4.为啥他循环了4次,从哪来的4次呢

还有个题目,问题写在注释里了

<br />var tmp = 'a';<br /><br />if([] == false) tmp += 'b';//这里的[]是啥,数组吗<br /><br />if(![]) tmp += 'c';//这里的[]是啥,数组吗<br /><br />alert(tmp); //tmp 值为 ?<br /><br />A.a<br />B.ab<br />C.ac<br />D.abc<br />



------解决方案--------------------
$arr = array(
    0 =>1,
    'aa' => 2,
    3,
    4
);
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn