Php 如何使一个字符串成为其他两个字符串的值?

Php 如何使一个字符串成为其他两个字符串的值?,php,Php,这是一个非常基本的问题,但我的方法看起来非常混乱,我想知道是否还有其他更干净的方法来实现这一点 $title = 'Post title Goes Here'; $author = 'Post author Goes Here'; $post_title = ''.$title.' - '.$author.''; 我怎样才能使$post\u title看起来不那么难看?但是有同样的目的吗?另一种方法是使用数组并内爆。 $post_title = "$title - $author"; 这将使

这是一个非常基本的问题,但我的方法看起来非常混乱,我想知道是否还有其他更干净的方法来实现这一点

$title = 'Post title Goes Here';
$author = 'Post author Goes Here';
$post_title = ''.$title.' - '.$author.'';

我怎样才能使
$post\u title
看起来不那么难看?但是有同样的目的吗?

另一种方法是使用数组并内爆。
$post_title = "$title - $author";
这将使变量“组合在一起”,并且通常更容易编码

$post["title"] = 'Post title Goes Here';
$post["author"] = 'Post author Goes Here';

$post_title = implode(" - ", $post);
// Or to keep it grouped:
$post["post_title"] = implode(" - ", $post);

另一种方法是使用数组并内爆。
这将使变量“组合在一起”,并且通常更容易编码

$post["title"] = 'Post title Goes Here';
$post["author"] = 'Post author Goes Here';

$post_title = implode(" - ", $post);
// Or to keep it grouped:
$post["post_title"] = implode(" - ", $post);
你可以用。从文档中:

当字符串用双引号或heredoc指定时,变量 在其中进行解析

如果要将变量与
-
连接起来,可以使用单引号:

$post_title = $title.' - ' .$author;
还可以使用返回格式化字符串

$post_title =  sprintf("%s - %s", $title, $author);
你可以用。从文档中:

当字符串用双引号或heredoc指定时,变量 在其中进行解析

如果要将变量与
-
连接起来,可以使用单引号:

$post_title = $title.' - ' .$author;
还可以使用返回格式化字符串

$post_title =  sprintf("%s - %s", $title, $author);

您希望从字符串内容中得到什么?“不那么难看?你想要达到什么?”对你来说意味着什么?
$post\u title=$title.-'.$author;
你实际上不需要第一个和最后一个引号组。你对字符串内容有什么期望?对你来说“不那么难看?你想要达到什么?”意味着什么?
$post\u title=$title.-'.$author您实际上不需要第一个和最后一个报价组。