Try to see if you will be able to answer a few questions on PHP.

Source: Internet
Author: User
This article mainly introduces a few questions about PHP. try to see if you will be lucky. these questions are all blind-eyed and you need a pair of eye-catching eyes, if you need it, you can refer to the following questions that you can see on the German Q & A. it feels interesting to share with you the traps and see if you will fall into it.

Question 1

The code is as follows:


$ 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:

The code is as follows:


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:

The code is as follows:


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


The answer is 1534.

Question 2

The code is as follows:


$ 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.

The code is as follows:


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:

The code is as follows:

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

The code is as follows:


$ 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

The code is as follows:


$ 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

The code is as follows:


$ A = count ("567") + count (null) + count (false );
Echo $;

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.