Home Backend Development PHP Tutorial PHP您可能也会掉入的坑

PHP您可能也会掉入的坑

Jun 13, 2016 pm 12:37 PM
here isset var

PHP你可能也会掉入的坑

今天被人问:

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

会不会输出'reach here!!!'? ---当然不会啊。我想也没想就答。

果然,我掉坑里了!会输出的!如果你没掉坑里,那么恭喜你,你也不用往下看了。

现在,让我们来分析一下。既然 isset 了,那么值到底是什么呢?我们把它打印出来:

var_dump($var['somekey']);
//=>output:  string(1) "t"
Copy after login
是字符't',就是 $var 的第一个字符,到这里想明白了吗?

因为变量$var是一个字符串,学过C语言的话就知道它是char类型的数组,所以我们可以用 $var[0] $var[1] $var[$i]...取到 $var 的第 $i+1 个字符。那么刚才的 'somekey' 为什么取到的是第一个字符呢?这是因为php在这里做了隐式的类型转换,将这里的字符串转换成int型。你试过intval('somekey')函数的话就知道得到的就是0,所以 $var['somekey']最终就是 $var[0]了。最后,得到了 't'。

完。


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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP determines whether a specified key exists in an array PHP determines whether a specified key exists in an array Mar 21, 2024 pm 09:21 PM

PHP determines whether a specified key exists in an array

The role and examples of var keyword in PHP The role and examples of var keyword in PHP Jun 28, 2023 pm 08:58 PM

The role and examples of var keyword in PHP

18 Ways to Fix Audio Service Not Responding Issue on Windows 11 18 Ways to Fix Audio Service Not Responding Issue on Windows 11 Jun 05, 2023 pm 10:23 PM

18 Ways to Fix Audio Service Not Responding Issue on Windows 11

How to bind domain name and set root directory to nginx server How to bind domain name and set root directory to nginx server May 13, 2023 pm 09:10 PM

How to bind domain name and set root directory to nginx server

Let's talk about the differences between var, let and const (code example) Let's talk about the differences between var, let and const (code example) Jan 06, 2023 pm 04:25 PM

Let's talk about the differences between var, let and const (code example)

Solution to the error AttributeError(\'{0!r} object has no attribute {1!r}\'.format(type(self).__name__, k)) Solution to the error AttributeError(\'{0!r} object has no attribute {1!r}\'.format(type(self).__name__, k)) Feb 29, 2024 pm 06:40 PM

Solution to the error AttributeError(\'{0!r} object has no attribute {1!r}\'.format(type(self).__name__, k))

What does let var const mean? What does let var const mean? Nov 14, 2023 pm 03:00 PM

What does let var const mean?

Over a thousand Android apps have made their way to Windows 11 today Over a thousand Android apps have made their way to Windows 11 today May 09, 2023 pm 07:01 PM

Over a thousand Android apps have made their way to Windows 11 today

See all articles