0

I would like to pass a parameter to a function. However, inside this parameter, I use variables that are defined inside the function. For example:

<?php

function foo($var){
    $test = "test";
    echo $var;
}

foo($test);

?>

In this example, I would like for the function to print out "test". Of course, this returns an error. However, this is what I am trying to do.

4

1 回答 1

2

您可以使用 using 来完成此操作variable-variables,但我不明白您为什么想要/需要:

function foo($var){
    $test = "test";
    echo $$var;
}

foo('test');

注意在echo里面的语句中foo(),变量有两个$前导。这是 avariable-variable$var将被视为变量名。因此,通过调用foo('test');(在这种情况下参数是字符串),$$var将评估为$test.

于 2012-10-18T05:07:28.603 回答