如何“数组解引用"?从PHP 7.2.0版本开始处理boolean/integer/float/string类型的标量值? [英] How does the "Array dereferencing" work on a scalar value of type boolean/integer/float/string as of PHP version 7.2.0?

查看:214
本文介绍了如何“数组解引用"?从PHP 7.2.0版本开始处理boolean/integer/float/string类型的标量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP 7.2.我从PHP手册 arrays章节中碰到了以下注释

数组无声地解引用了不是字符串的标量值 产生 NULL ,即不发出错误消息.

我了解如何取消引用数组文字,但是我无法理解数组取消引用"如何处理布尔值/整数/浮点数/字符串类型的标量值?

如果您查看PHP手册本身的代码示例,则会发现矛盾之处,因为它不是整数类型的值,并非根据手册默默地产生NULL.

<?php
function getArray() {
    return array(1, 2, 3);
}
$secondElement = getArray()[1];
var_dump($secondElement); // int(2)
//According to the manual I expected it to be NULL as it's not of type string

取消引用类型为 boolean/integer/float 的标量值与取消引用类型为 string 的值有何不同?

解决方案

它们是指非复杂类型,例如int或float.

在您的示例中,您正在使用数组.所以您看不到问题.

<?php
function getArray() {
    return array(1, 2222, 3);
}
$secondElement = getArray()[1]; // 2222

$null = $secondElement[123456]; // 123456 can be any number or string
var_dump($null);

// similarly:
$also_null = getArray()[1][45678];
var_dump($also_null);

第一对括号是在数组(1、2222、3)上进行数组解引用,第二对括号是在始终返回null的整数(2222)上进行数组解引用.

简体:

<?php
$a = 123456;
var_dump($a[42]); // is null
$a = 123.45;
var_dump($a[42]); // is null
$a = true;
var_dump($a[42]); // is null
$a = null;
var_dump($a[42]); // is null

此操作默默失败",因为从理论上讲您应该从中得到一个错误,而不仅仅是空.

除了int,float和bool之外,还使用null来发生:

<?php 
$a = true;
var_dump($a[42][42][42][42][42][42][42][42]); // also null, and no errors

但是可以正确地使用数组和字符串.

<?php
$a = "abc";
var_dump($a[1]); // b
$a = [11, 22, 33];
var_dump($a[1]); // 22

回答您的问题在类型的标量值上,数组取消引用" 如何工作":不会,它只是返回null而不是返回某种错误./p>

I am using PHP 7.2. I come across the following note from the arrays chapter of PHP Manual

Array dereferencing a scalar value which is not a string silently yields NULL, i.e. without issuing an error message.

I understand how to dereference an array literal but I'm not able to understand how the "array dereferencing" works on a scalar value of type boolean/integer/float/string?

If you look at the code example from the PHP manual itself, you can notice the contradiction as it's not the value of integer type is not silently yielding NULL according to the manual.

<?php
function getArray() {
    return array(1, 2, 3);
}
$secondElement = getArray()[1];
var_dump($secondElement); // int(2)
//According to the manual I expected it to be NULL as it's not of type string

How is dereferencing a scalar value of type boolean/integer/float different from dereferencing the value of type string?

解决方案

They are referring to non-complex types such as int or float.

In your example you are using an array. So you don't see the issue.

<?php
function getArray() {
    return array(1, 2222, 3);
}
$secondElement = getArray()[1]; // 2222

$null = $secondElement[123456]; // 123456 can be any number or string
var_dump($null);

// similarly:
$also_null = getArray()[1][45678];
var_dump($also_null);

The first pair of brackets is array-dereferencing on the array (1, 2222, 3), the second is array-dereferencing on an integer (2222) which always returns null.

Simplified:

<?php
$a = 123456;
var_dump($a[42]); // is null
$a = 123.45;
var_dump($a[42]); // is null
$a = true;
var_dump($a[42]); // is null
$a = null;
var_dump($a[42]); // is null

This "fails silently" as in theory you should get an error from this, rather than just null.

Also happens with null, other than int, float, bool:

<?php 
$a = true;
var_dump($a[42][42][42][42][42][42][42][42]); // also null, and no errors

But works correctly instead with arrays and strings.

<?php
$a = "abc";
var_dump($a[1]); // b
$a = [11, 22, 33];
var_dump($a[1]); // 22

Answering your question, "How does the "Array dereferencing" work on a scalar value of type": It doesn't, it just returns null instead of returning an error of some sort.

这篇关于如何“数组解引用"?从PHP 7.2.0版本开始处理boolean/integer/float/string类型的标量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆