在 PHP 中使用带有动态变量名的大括号 [英] Using braces with dynamic variable names in PHP
本文介绍了在 PHP 中使用带有动态变量名的大括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用动态变量名称(我不确定它们的实际名称)但非常像这样:
I'm trying to use dynamic variable names (I'm not sure what they're actually called) But pretty much like this:
for($i=0; $i<=2; $i++) {
$("file" . $i) = file($filelist[$i]);
}
var_dump($file0);
返回的是 null
这告诉我它不起作用.我不知道我在这里寻找的语法或技术是什么,这使得研究变得困难.$filelist
已在前面定义.
The return is null
which tells me it's not working. I have no idea what the syntax or the technique I'm looking for is here, which makes it hard to research. $filelist
is defined earlier on.
推荐答案
将它们包裹在 {}
中:
${"file" . $i} = file($filelist[$i]);
工作示例
<小时>使用${}
是一种创建动态变量的方式,简单例子:
Working Example
Using ${}
is a way to create dynamic variables, simple example:
${'a' . 'b'} = 'hello there';
echo $ab; // hello there
这篇关于在 PHP 中使用带有动态变量名的大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文