微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-没有名称的变量:${”}

因此存在variable variables.表示这有效

$a = 'test';
$$a = 'Hello';
echo ${'test'}; //outputs 'Hello'

但是现在我遇到了一些使用不带名称的变量的奇怪代码

function test(&$numRows) {
    $numRows = 5;
    echo ' -- done test';
}

$value = 0;
test($value);
echo ' -- result is '.$value;

test(${''}); //variable without name

http://ideone.com/gTvayV Code fiddle

输出为:

— done test — result is 5 — done test

这意味着代码不会崩溃.

现在我的问题是:如果参数是没有名称的变量,则更改$numRows值会发生什么?价值会被写入必杀技吗? PHP变量等于/ dev / null吗?
 我找不到关于此的任何具体信息.

提前致谢

解决方法:

Now my question is: what exactly happens if $numRows value is changed
when the parameter is a variable without name?

没有没有名称的变量,PHP中的空字符串是完全有效的名称.

也许我错了,但是在PHP中,所有变量都可以通过它们的名称(或更确切地说,是其名称的字符串表示形式)进行访问,并且由于空字符串仍然是字符串,因此将其视为有效名称.

考虑像数组键值对这样的变量.您可以使用空字符串创建数组键:

$arr = [];
$arr[''] = 'appul';
var_dump($arr['']); // prints: string(5) "appul"
$arr[''] = 'ponka';
var_dump($arr['']); // prints: string(5) "ponka"

每当您访问$arr [”]时,您都将输入相同的值.

您也可以使用$GLOBAL变量将所有变量作为字符串访问,因此您可以检查“无名”变量发生了什么:

${''} = 'ponka';
var_dump($GLOBALS['']); // prints: string(5) "ponka"
${''} = 'appul';
var_dump($GLOBALS['']); // prints: string(5) "appul"

Will the value be written into nirvana? Is that the PHP variable equivalent to /dev/null? I wasn’t able to find anything specific about this.

不,它不会成为必杀技,它静静地坐落在全球空间中,并且访问它有点棘手,但除此之外,它是与其他变量一样的普通变量.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐