开发者

PHP How do I retrieve this value?

开发者 https://www.devze.com 2023-02-12 21:46 出处:网络
I have a situation where I need to get the value of a variable, where the name od the variable is passed in a string in a second variable.

I have a situation where I need to get the value of a variable, where the name od the variable is passed in a string in a second variable.

For example

$abc = 'the value I want';

$def = 'abc';

Where $def is the only variable name I can garrauntee being accessible. In this situation how can I get the value of $abc.

Cheer开发者_Python百科s


echo $$def; does the job.

Use it very sparsely though, it makes code difficult to maintain!


PHP can have variable variables, like this:

${$def}

It also, incidentally, can have variable object and function names.


You can use "$$def" for this.

Examples: http://php.net/manual/en/language.variables.variable.php

0

精彩评论

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