Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何重新解析包含变量名的变量字符串?_Php - Fatal编程技术网

Php 如何重新解析包含变量名的变量字符串?

Php 如何重新解析包含变量名的变量字符串?,php,Php,我想要$c来回报“我是个男孩” 这是我的问题的一个简单例子。在实际情况中,涉及到许多变量。有一个简单的解决方法吗?这就是函数派上用场的地方。它们接受参数并返回一些编译后的值 $a = 'i am a $b'; // declared before $b is declared function x(....) { global $a; $b = 'boy'; $c = '{$a}'; // i know this doesn't work. how can I make

我想要$c来回报“我是个男孩”


这是我的问题的一个简单例子。在实际情况中,涉及到许多变量。有一个简单的解决方法吗?

这就是函数派上用场的地方。它们接受参数并返回一些编译后的值

$a = 'i am a $b'; // declared before $b is declared

function x(....) {
    global $a;
    $b = 'boy';
    $c = '{$a}'; // i know this doesn't work. how can I make it work?
}
如果确实需要使用变量的内容重新分析某些字符串,只需使用
str\u ireplace

// create named function
function namedFn($b) {
    return "i am a $b";
}
//  or anonymous
$f = function ($b) {return "i am a $b"; };

// call function and pass $b as argument
$b = 'boy';
echo namedFn($b);
echo $f($b);

eval是邪恶的,但有时它是你需要的。。。
$a = 'i am a $b';
echo str_ireplace('$b', $b, $a); // $search , $replace , $subject