A few tricky PHP questions. Try to see if you will fall into the trap.

Source: Internet
Author: User

A few tricky PHP questions. Try to see if you will fall into the trap.

I have seen these questions on the German Q & A, and they are very interesting. I will share with you the traps to see if you will fall into it.

Question 1

$arr = array(0=>1,"aa"=>2, 3, 4);foreach($arr as $key=>$val){    print($key == "aa" ? 5 : $val);}

What is the output result? If the answer is 1534, it will fall into the trap.

Let's take a look at the final structure of this array:

Array (
[0] => 1
[Aa] => 2
[1] => 3
[2] => 4
)

Then, traverse the key of each element to see if the key is not equal to aa. If the key is equal to aa, use 5 instead. Will you be a little surprised when I tell you the answer is 5534! Is 0 equal to "aa? Yes, 0 is equal to "aa". The key point of this question is to test you. When two values in PHP are used for logical judgment, if the two values are of different types, PHP will automatically convert the value on the right to the type on the left, and then judge. Therefore, "aa" is converted to 0, which is equal to 0 on the left. You can avoid this situation by using full equals, that is, if you write:

Print ($ key = "aa "? 5: $ val );

The answer is 1534.

Question 2

$i='11';printf("%d\n",printf("%d",printf("%d",$i)));

What is the output result? If the answer is "11" or "111111", it will fall into the trap.

First understand the printf function. printf is not only a printing function, but also a return value. This is the focus.

var_dump(printf("%d",$i));

Guess what the above result is? First, printf prints the variable itself 11, and then printf returns a variable String Length value. 11 has two characters, SO 2 is returned, so the execution result of the preceding statement is equal:

11int (2)

After clarifying this point, I will go back to the above questions. According to the priority, the restrictive deep printf function prints 11 and returns 2. The second-level printf function prints 2 and returns 1. At the last layer, print 1 directly, so the execution result is 1121.

Question 3

$a = 3;$b = 5;if($a = 5 || $b = 7) {    $a++;    $b++;}echo $a . " " . $b;

What is the execution result? If you answer 6 8 or 4 6 or 6 6, you will fall into the trap.

The first trap is that the answer is 4 6. You may think of $ a = 5 | $ B = 7 as $ a = 5 | $ B = 7, which is a common mistake for new users.

The second trap is that the answer is 6 8. You saw the scam $ a = 5 | $ B = 7, but you didn't notice that, in logic or, you only need to execute it until the result of an expression is true, if the expression is followed by $ a = 5, true is returned, and $ B = 7 is not followed.

The third trap is that the answer is 6 6. OK, you have understood the logic or rules, so $ a = 5 is executed, $ B = 7 is not executed, but you didn't consider this as a logical expression, the value returned to $ a is converted to a Boolean value. In this case.

So after the above three traps, you should know the answer. In fact, after $ a is equal to true, the echo $ a output is 1, the $ B value remains unchanged, and the result is 1 6.

Question 4

$count = 5;function get_count() {    static $count = 0;    return $count++;}++$count;get_count();echo get_count();

What is the execution result? If you answer 2, congratulations, you have fallen into the trap.

In fact, this question mainly involves two points. The first point is the static type. This value is always static. The first call declaration is equal to 0 and the auto increment is equal to 1. For the second call, 1 and then auto-increment are equal to 2. However, there is still a trap here, that is, the difference between ++ a and a ++. The first ++ is auto-increment, And the last ++ is the first return value and then auto-increment, therefore, the result is equal to 1.

Question 5

$a = count ("567")  + count(null) + count(false);echo $a;

If you answer 3 or 1, congratulations, it's a trap.

Because count (null) is equal to 0, false is also a value. Therefore, count (false) equals 1.

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.