PHP - 如何正确使用带变量的大括号?
我读了 php 文档,我看到了这个:
class foo{
var $bar = 'I am a bar';
}
$foo = new foo();
$identity = 'bar';
echo "{$foo->$identity}";
我看到有人这样写:
if (!isset($ns->job_{$this->id})){
//do something
}
但是当我尝试使用这段代码时,它不起作用:
$id1 = 10;
$no = 1;
echo ${id.$no};
你们能告诉我为什么它不起作用以及何时可以将大括号与变量一起使用正确吗?
I read php document and I saw this:
class foo{
var $bar = 'I am a bar';
}
$foo = new foo();
$identity = 'bar';
echo "{$foo->$identity}";
And I saw somebody wrote like this:
if (!isset($ns->job_{$this->id})){
//do something
}
But when I tried with this code, It didn't work:
$id1 = 10;
$no = 1;
echo ${id.$no};
Can you guys tell me why it didn't work and when I can use braces with variable correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实例
括号可用于对象类型,例如,模拟数组索引。假设
$arr
是一个数组类型,$obj
是一个对象,我们有:你可以让它变得更有趣,例如:
甚至更多:
编辑:你的问题 --
变量的变量
Live example
Brackets can be used on object types, for instance, to simulate a array index. Supposing that
$arr
is an array type and$obj
an object, we have:You can make it more fun, for instance:
Even more:
Edit: Your problem --
variable of variable
您期待什么?
What are you expecting?