Home >Backend Development >PHP Tutorial >Pitfall 1 that PHP may fall into

Pitfall 1 that PHP may fall into

小云云
小云云Original
2018-03-19 14:57:531273browse

This article mainly shares with you the pitfalls that PHP may fall into. We share the answer with you through a small example, hoping to help everyone.

I was asked today:

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

Will it output 'reach here!!!'? ---Of course not. I answered without thinking.

Sure enough, I fell into a trap! It will be output! If you didn't fall into the trap, congratulations, you don't need to look down.

Now, let us analyze it. Now that isset is set, what is the value? Let's print it out:

var_dump($var['somekey']);
//=>output:  string(1) "t"

is the character 't', which is the first character of $var. Have you figured it out here?

Because the variable $var is a string, if you have learned C language, you will know that it is an array of char type, so we can use $var[0] $var[1] $var[$i]. ..Get the $i+1 character of $var. So why did the 'somekey' just get the first character? This is because PHP does implicit type conversion here, converting the string here into int type. If you have tried the intval('somekey') function, you will know that what you get is 0, so $var['somekey']In the end it is $var[0]. Finally, got 't'.

The above is the detailed content of Pitfall 1 that PHP may fall into. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn