PHP, you may also fall into the trap

Source: Internet
Author: User
PHP, you may also fall into the trap of being asked today: $ var = 'test'; if (isset ($ var ['somekey']) {echo 'reachhere !!! ';} Will output 'reachhere !!! '? --- You may also fall into a trap when using PHP.

Asked today:

$ Var = 'test'; if (isset ($ var ['somekey']) {echo 'reach here !!! ';}

Will it output 'reach here !!! '? --- Of course not. I want to answer the question without thinking about it.

Sure enough, I fell into the trap! Will output! If you haven't fallen into the trap, congratulations, and you don't have to look down.

Now, let's analyze it. What is the value of isset? We print it out:

Var_dump ($ var ['somekey']); // => output: string (1) "t"
It is the character 'T', which is the first character of $ var. do you want to understand it here?

Because the variable $ var is a string, if you have learned the C language, you will know that it is a char array, so we can use $ var [0] $ var [1] $ var [$ I]... get the $ I + 1 character of $ var. So why did we get the first character of 'somekey? This is because php implements implicit type conversion here and converts the string here to the int type. If you have tried the intval ('somekey') function, you will know that the result is 0. Therefore, $ var ['somekey'] is eventually $ var [0. Finally, we get 'T '.

.


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.