开发者

Getting a variable from an object with a string in PHP

I have a Persons object. It has a variable name accessible by saying

$p = new Person('John');
echo $p->name;

Now I have a string.

$name = 'name';

I need to get $p->name using $p and $name. Something like

echo $p->[$name];开发者_运维问答


echo $p->{$name};


echo $p->$name;


echo $p->$name; may generates error if it contains special characters so following one is perfect

echo $p->{$name};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜