如何在字符串中包含尚未定义的变量?PHP [英] How do I include a yet to be defined variable inside a string? PHP
本文介绍了如何在字符串中包含尚未定义的变量?PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为了执行更少的数据库查询和代码的清晰性,我想在字符串中包含一个尚未定义的变量.稍后在页面中,将声明变量并打印和评估字符串.我该怎么做?
For sake of performing less database queries and for clarity of code, I'd like include a yet to be defined variable inside a string. Later in the page, the variable will be declared and the string printed and evaluated. How do i do this?
$str="This $variable is delicious";
$array=array("Apple","Pineapple","Strawberry");
foreach($array as $variable)
{
print "$str";
}
推荐答案
您可以使用 printf()
(或 sprintf()
如果不想回显的话):
You can use printf()
(or sprintf()
if you don't want to echo it):
$str = 'This %s is delicious';
foreach ($array as $variable) {
printf($str, $variable);
}
这篇关于如何在字符串中包含尚未定义的变量?PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文