PHP是否具有Python模板字符串之类的功能? [英] Does PHP have a feature like Python's template strings?
本文介绍了PHP是否具有Python模板字符串之类的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Python具有称为模板字符串的功能.
Python has a feature called template strings.
>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
我知道PHP允许您编写:
I know that PHP allows you to write:
"Hello $person"
并替换为$person
,但是模板可以在代码的各个部分中重复使用吗?
and have $person
substituted, but the templates can be reused in various sections of the code?
推荐答案
您还可以使用 strtr
:
You could also use strtr
:
$template = '$who likes $what';
$vars = array(
'$who' => 'tim',
'$what' => 'kung pao',
);
echo strtr($template, $vars);
输出:
tim likes kung pao
这篇关于PHP是否具有Python模板字符串之类的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文