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

Pitfall 1 that PHP may fall into

小云云
Release: 2023-03-21 17:42:01
Original
1266 people have browsed it

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!!!';
}
Copy after login

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"
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template