我有一個包含變數的字串,我想將變數的值添加到其中,因為我正在嘗試它,它只是作為字串吐出而不是添加變數的值。
這是我的代碼
$vars = $item->toArray();
extract($vars);
echo ($message_tmpl);die;
echo print_r($message_tmpl)die;
提取變數以添加值,但它回傳純輸出而不是值。
$first_name 是通過 $vars 提取的
Output
'My message to $first_name';
It should be
'My message to John Doe';
謝謝
uj5u.com熱心網友回復:
你可以這樣做 :
$name = "Toto";
$info["age"] = "8yo";
echo "Hello {$name} who is {$info["age"]}";
將輸出:
Hello Toto who is 8yo
您還可以使用 strtr() 函式,例如:
$template = '$who likes $what';
$vars = array(
'$who' => 'Toto',
'$what' => 'fruits',
);
echo strtr($template, $vars);
你會得到 :
Toto likes fruits
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/527325.html
標籤:php模板php-7
