回声变量中的字符串,从数据库getted [英] Echo a string with variable inside, getted from DB
问题描述
我从MySQL获得查询字符串,我已经将其打印到屏幕上。这个字符串,我需要打印一些变量太多。从数据库字符串可能是:
I get from a MySQL query a string and I've to print it to screen. Into this string, I need to print some variable too. The string from DB could be:
Finally, the team {$matchData['team_name']} have scored a point!
$ matchData ['TEAM_NAME']
是本场比赛的计算期间使用数组,如果我做一个简单的
$matchData['team_name']
is an array used during the calculation of the match, and if I do a simple
echo $matchData['team_name'];
将打印球队的正确名称(BOSTON在这种情况下)。但是为什么,如果我回应来自DB getted字符串,回声打印:
Finally, the team {$matchData['team_name']} have scored a point!
而不是
Finally, the team BOSTON have scored a point!
我在哪里失败?
推荐答案
PHP似乎只是在简单的情况来处理字符串变量替换...的万无一失的方法,以确保它始终工作原理是利用串联,如:
PHP seems to handle variable substitution in strings only in the simpler cases... the foolproof way to make sure it always works is to use concatenation, like:
echo "Finally, the team ".$matchData['team_name']." have scored a point!";
在PHP手册页的确,虽然举这个例子:
The PHP man page does give this example though:
// You can also use arrays
$baz = array("value" => "foo");
echo "this is {$baz['value']} !"; // this is foo !
所以,你可能要仔细检查你对这种语法了。
So you might want to double check your syntax against that, too.
最后,echo使用可变长度的参数列表,所以你也可以这样写:
Finally, echo takes a parameter list of variable length so you can also write:
echo "Finally, the team ", $matchData['team_name'], " have scored a point!";
这篇关于回声变量中的字符串,从数据库getted的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!