Try a few PHP interview questions in cheat people to see if you will also be recruited

  • 2021-07-12 05:20:56
  • OfStack

These questions are seen in German questions, and they feel very interesting. Let's share the traps and see if you will fall into them.

Question 1


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

What is the output? If the answer is 1534, you will fall into a trap.
Let's look at the final structure of this array:

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

Then traverse the key of every 1 element to see that the equality is not equal to aa, and if it is equal, replace it with 5. 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", which is the key point of this question. In PHP, if the two values are not 1, PHP will automatically convert the right value to the left type, and then make a judgment. Therefore, "aa" conversion shaping is equal to 0, which is naturally equal to 0 on the left. You can use all equals to avoid this situation, that is, if you write:

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

Then the answer is 1534.

Question 2


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

What is the output? If you answer 11, or 111111, you fall into a trap.
Let's start with the function printf. printf is not only a print function, but also a return value. That's the point

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

Can you guess what the result above is? First printf prints the variable itself 11, and then printf returns a value of 1 variable string length, 11 has two characters, and returns 2, so the execution result of the above statement is equal to:
11int(2)

After clarifying this point, look back at the above test questions, print 11 according to the priority and restrictive depth printf function, and return 2. Then go to the level 2 printf function, print 2 and return 1. Finally, go to layer 3 and 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 fall into a trap.
The first trap is that the answer is equal to 4 6. It is estimated that you are careless to regard $a = 5 $b = 7 as $a = = 5 $b = = 7, which is a common mistake made by novices.

The second trap is that the answer is equal to 6 8. You see through the $a = 5 $b = 7 scam, but you don't notice that the logic is executed in turn until an expression results in true, and the following expression is no longer executed, $a = 5 returns true, and the following $b = 7 is not executed.
The third trap, think the answer is equal to 6 6. OK, you see through the rules of logical OR, so $a=5 executes and $b=7 does not, but you don't consider that this is a logical expression, and the value returned to $a is to be converted to a Boolean value. Look at it this way.

So after the above three traps, you should know what the answer is. In fact, after $a equals true, the output of echo $a is 1, and the value of $b remains unchanged, resulting in 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 a trap.
In fact, this question mainly tests two points, and the first point is static static type. The value of this is always static, declared equal to 0 on the first call, and incremented equal to 1. For the second call, 1 is equal to 2 if it is added by itself. But in fact, there is another trap here, that is, the difference between + + a and a + +. The former + + is self-increasing, and the latter + + is returning the value first and then self-increasing, so the result is equal to 1.

Question 5


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

If you answer 3 or 1, congratulations, falling into a trap.
Because count (null) is equal to 0, false is also a value. So count (false) is equal to 1.


Related articles: