A few deceptive php face test questions try to see if you will also recruit _php instance

Source: Internet
Author: User

These questions are seen in the German question, it is very interesting, bring to share the trap, see you will not fall into it.

First question.

Copy Code code as follows:

$arr = Array (0=>1, "AA" =>2, 3, 4);

foreach ($arr as $key => $val) {
Print ($key = = "AA"? 5: $val);
}

How much is the output result? If the answer is 1534, it falls into the trap.
Let's take a look at the structure that the array eventually forms:
Copy Code code as follows:

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

And then traverse each element of the key to see not equal to AA, equal to 5 substitution. When I tell you the answer is 5534, you won't be a little surprised! is 0 equal to "AA"? Yes, 0 is equal to "AA", this question focuses on the test you this. When two values are logically judged in PHP, if the type of two values is inconsistent, PHP automatically converts the value on the right to the type on the left, and then makes the decision. So the "AA" transformation is equal to 0, natural is equal to the left 0. You can use all equal to avoid this situation, i.e. if you write:
Copy Code code as follows:

Print ($key = = = "AA" 5: $val);

So the answer is 1534.

Second question

Copy Code code as follows:

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

How much is the output result? If your answer is 11, or 111111, it falls into the trap.
To understand this function, printf is not just a print function, it also has a return value. That's the point.
Copy Code code as follows:

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

You know what the result is? First printf The print variable itself 11, then printf returns a variable string length value, 11 has two characters, then returns 2, so the execution result of the above statement equals:
Copy Code code as follows:
11int (2)

Clear this point, and then back to look at the above questions, according to the priority, restrictive depth printf function, print 11, return 2. Next to the second-level printf function, print 2, and return 1. Finally to the third layer, directly print 1, so the execution result is 1121.

Question number three.

Copy Code code 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, then you fall into the trap.
The first trap, think the answer equals 4 6.  I guess you carelessly put $a = 5 | |  $b = 7 as $a = = 5 | | $b = = 7, this is the novice often make mistakes.

The second trap, that the answer equals 6 8.  You see through $a = 5 | | $b = 7 This scam, but you did not notice that in logic or in a sequence until the result of an expression is true, the expression behind it is no longer executed, $a = 5 returns True, and the $b=7 behind it is not executed.
The third trap is that the answer equals 6 6. OK, you see through the logic or the rules, so $a=5 execution, $b =7 not execute, but you do not take into account that this is a logical expression, the value returned to $a is converted to a Boolean value. Look at that.

So after the above three traps, you should know the answer is how much, in fact $a equals true, echo $a output is 1, $b value unchanged, the result is 1 6.

Question fourth.

Copy Code code 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 fall into the trap.
In fact, this problem mainly test two points, the 1th is static statically type. This value is always static, the first invocation declaration equals 0, and the self increment equals 1. The second call, 1 and then the increase is equal to 2. But in fact there is a trap, that is the difference between ++a and a++, before + + is the first to increase, after + + is the first return value and then increase, so the result is equal to 1.

Question fifth.

Copy Code code as follows:

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

If you answer 3 or 1, congratulations, fall into the trap.
Because count (null) equals 0,false, it also counts as a value. So 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.