开发者

How to get the value of a variable which is partially created from a string?

$prefix = 'some';
$name_of_va开发者_StackOverflowriable = $prefix.'_var';

So I have a variable named $some_var.

How can I check the value of it?

if($name_of_variable) ...

will return the value of $name_of_variable instead of the value of $name_of_variable.


Variable variables. But you do NOT want to use them. They make for impossible-to-debug code. They're almost always a sign of bad design.


DO NOT use a variable which is partially created from a string.
Use arrays instead.

$prefix = 'some';
$name_of_variable = 'var';
echo $array[$prefix][$name_of_variable];


Variable variable usually used when you need to create variables from string,for example convert $_POST keys into variable with its value .

$allowed_var = array('name',..);
foreach( $_POST as $key => $value
{
   if( isset($allowed_var[$key] ) )
        ${$key} = $value;
}
...
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜