PHP您可能也会掉入的坑
Jun 13, 2016 pm 12:37 PM
PHP你可能也会掉入的坑
今天被人问:
$var = 'test'; if (isset($var['somekey'])) { echo 'reach here!!!'; }
会不会输出'reach here!!!'? ---当然不会啊。我想也没想就答。
果然,我掉坑里了!会输出的!如果你没掉坑里,那么恭喜你,你也不用往下看了。
现在,让我们来分析一下。既然 isset 了,那么值到底是什么呢?我们把它打印出来:
var_dump($var['somekey']); //=>output: string(1) "t"
因为变量$var是一个字符串,学过C语言的话就知道它是char类型的数组,所以我们可以用 $var[0] $var[1] $var[$i]...取到 $var 的第 $i+1 个字符。那么刚才的 'somekey' 为什么取到的是第一个字符呢?这是因为php在这里做了隐式的类型转换,将这里的字符串转换成int型。你试过intval('somekey')函数的话就知道得到的就是0,所以 $var['somekey']最终就是 $var[0]了。最后,得到了 't'。
完。

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP determines whether a specified key exists in an array

The role and examples of var keyword in PHP

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

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)

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

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