Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the fallible face test questions for PHP?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what are the PHP error-prone interview questions". In the daily operation, I believe that many people have doubts about what PHP error-prone interview questions are. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "what are the PHP error-prone interview questions?" Next, please follow the editor to study!

The first question

The copy 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? If the answer is 1534, you will fall into a trap.

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

The copy code is as follows:

Array

(

[0] = > 1

[aa] = > 2

[1] = > 3

[2] = > 4

)

Then traversing the key of each element is not equal to aa, but instead of 5. Will you be a little surprised when I tell you the answer is 5534? Is 0 equal to "aa"? Yes, 0 equals "aa". That's the key point of this question. When making a logical judgment between two values in PHP, if the type of the two values is not the same, PHP will automatically convert the value on the right to the type on the left, and then make the judgment. So "aa" conversion shaping equals 0, which naturally equals 0 on the left. You can use it all to avoid this situation, that is, if you write:

The copy code is as follows:

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

Then the answer is 1534.

Second question

The copy code is as follows:

$iTunes 11'

Printf ("% d\ n", printf ("% d", printf ("% d", $I))

What is the output? If your answer is 11, or 111111, you will fall into a trap.

Let's take a look at the function printf. Printf is not just a print function, it also returns a value. That's the point.

The copy code is as follows:

Var_dump (printf ("% d", $I))

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

The copy code is as follows:

11int (2)

Once this is clear, go back to the above question and print 11 and return 2 according to the priority, restricted depth printf function. Then go to the second level printf function, print 2, and return 1. Finally, to the third layer, print 1 directly, so the execution result is 1121.

The third question

The copy code is as follows:

$a = 3

$b = 5

If ($a = 5 | | $b = 7) {

$asides +

$baked +

}

Echo $a. "". $b

What is the result of the implementation? If you answer 6 8 or 4 6 or 6 6, you are falling into a trap.

The first trap is that the answer is equal to 4 / 6. It is estimated that you carelessly regard $a = 5 | | $b = 7 as $a = = 5 | | $b = = 7, which is a common mistake made by beginners.

The second trap is that the answer is equal to 68. You see through the scam of $a = 5 | | $bread7, but you haven't noticed that as long as the logic is executed in turn until the result of an expression is true, the one after the expression is no longer executed, $a = 5 returns true, and the following $bread7 is not executed.

The third trap is that the answer is equal to 66. OK, you see through the rule of logical OR, so $axi5 executes, $bread7 doesn't execute, but you don't take into account that this is a logical expression, and the value returned to $an is converted to a Boolean. Look at it this way.

So after the above three traps, you should know what the answer is. In fact, after $an equals true, the output of echo $an is 1, the value of $b remains the same, and the result is 16.

Question 4

The copy code is as follows:

$count = 5

Function get_count () {

Static $count = 0

Return $count++

}

+ + $count

Get_count ()

Echo get_count ()

What is the result of the implementation? If you answer 2, congratulations, you've fallen into a trap.

In fact, there are two main points in this question. the first point is the static type of static. This value is always static, with the first call declared equal to 0 and the self-increment equal to 1. On the second call, 1 increments itself to 2. But in fact, there is another trap, that is, the difference between + + an and await +. The first + + increases itself first, and then + + returns the value and then increases itself, so the result is equal to 1.

Question 5

The copy code is as follows:

A = count (567) + count (null) + count (false)

Echo $a

If you answer 3 or 1, congratulations, you've fallen into a trap.

Because count (null) equals zero false is also a value. So count (false) equals 1.

At this point, the study of "what are the PHP error-prone interview questions" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report