相关疑难解决方法(0)

PHP中的字符串中的大括号

{ }PHP中字符串文字中(花括号)的含义是什么?

php string

181
推荐指数
3
解决办法
10万
查看次数

PHP 有没有像 Python 的 f-string 函数那样的字符串函数?(不是 str.format())

我是 PHP 新手,所以请原谅这个问题。

我想知道 PHP 是否有字符串格式函数,例如 Python 的 f-strings 函数,而不是 str.format()。我看过一些关于这个主题的帖子,但大多数被接受为答案的例子是指 Python 处理格式化字符串的旧方式str.format()。在我的情况下,我想使用格式化的字符串构建一个变量,例如(Python):

f_name = "John"
l_name = "Smith"
sample = f`{f_name}'s last name is {l_name}.`
print(sample)
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用(PHP):

 $num = 5;
 $location = 'tree';
 $format = 'There are %d monkeys in the %s';
 echo sprintf($format, $num, $location);
Run Code Online (Sandbox Code Playgroud)

但是如果我想$format用作变量呢?主要思想是基于其他变量创建一个动态变量,例如:

$db_type = $settings['db_type'];  # mysql
$db_host = $settings['db_host'];  # localhost
$db_name = $settings['db_name'];  # sample

var $format = "%s:host=%s; dbname=%s";

# Not sure what to do after that, …
Run Code Online (Sandbox Code Playgroud)

php string f-string

5
推荐指数
1
解决办法
2695
查看次数

标签 统计

php ×2

string ×2

f-string ×1