如何在 PHP 中将两个字符串组合在一起?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8336858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to combine two strings together in PHP?
提问by glennanyway
I don't actually know how to describe what I wanted but I'll show you:
我实际上不知道如何描述我想要的,但我会告诉你:
For example:
例如:
$data1 = "the color is";
$data2 = "red";
What should I do (or process) so $result is the combination of $data1and $data2?
我应该怎么做(或过程),这样$结果的组合$data1和$data2?
Desired result:
想要的结果:
$result = "the color is red";
回答by Logan Serman
$result = $data1 . $data2;
This is called string concatenation. Your example lacks a space though, so for that specifically, you would need:
这称为字符串连接。不过,您的示例缺少空格,因此具体而言,您需要:
$result = $data1 . ' ' . $data2;
回答by John Conde
There are several ways to concatenate two strings together.
有几种方法可以将两个字符串连接在一起。
Use the concatenation operator .(and .=)
使用连接运算符.(和.=)
In PHP .is the concatenation operator?which returns the concatenation of its right and left arguments
在 PHP 中.是连接运算符?它返回其左右参数的连接
$data1 = "the color is";
$data2 = "red";
$result = $data1 . ' ' . $data2;
If you want to append a string to another string you would use the .=operator:
如果要将一个字符串附加到另一个字符串,您可以使用.=运算符:
$data1 = "the color is ";
$data1 .= "red"
Complex (curly) syntax / double quotes strings
复杂(卷曲)语法/双引号字符串
In PHP variables contained in double quoted strings are interpolated (i.e. their values are "swapped out" for the variable). This means you can place the variables in place of the strings and just put a space in between them. The curly braces make it clear where the variables are.
在 PHP 中,包含在双引号字符串中的变量是内插的(即它们的值被“换出”变量)。这意味着您可以将变量放置在字符串的位置,并在它们之间放置一个空格。花括号清楚地表明变量的位置。
$result = "{$data1}?{$data2}";
Note: this will also work without the braces in your case:
注意:在您的情况下,这也可以在没有大括号的情况下工作:
$result = "$data1 $data2";
Use sprintf()or printf()
使用sprintf()或printf()
sprintf()allows us to format strings using powerful formatting options. It is overkill for such simple concatenation but it handy when you have a complex string and/or want to do some formatting of the data as well.
sprintf()允许我们使用强大的格式化选项来格式化字符串。对于这种简单的连接来说,这有点矫枉过正,但是当您有一个复杂的字符串和/或还想对数据进行一些格式化时,它会很方便。
$result?= sprintf("%s %s", $data1, $data2);
printf()does the same thing but will immediately display the output.
printf()做同样的事情,但会立即显示输出。
printf("%s %s", $data1, $data2);
// same as
$result?= sprintf("%s %s", $data1, $data2);
echo $result;
Heredoc
赫里多克
Heredocscan also be used to combine variables into a string.
Heredocs也可用于将变量组合成一个字符串。
$result= <<<EOT
$data1 $data2
EOT;
Use a ,with echo()
使用,与echo()
This only works when echoing out content and not assigning to a variable. But you can use a comma to separate a list of expressions for PHP to echo out and use a string with one blank space as one of those expressions:
这仅在回显内容而不是分配给变量时有效。但是您可以使用逗号分隔 PHP 的表达式列表以回显并使用带有一个空格的字符串作为这些表达式之一:
echo $data1, ' ', $data2;
回答by codelame
No one mentioned this but there is other possibility. I'm using it for huge sql queries. You can use .= operator :)
没有人提到这一点,但还有另一种可能性。我将它用于大型 sql 查询。您可以使用 .= 运算符 :)
$string = "the color is ";
$string .= "red";
echo $string; // gives: the color is red
回答by Jeremy Ruten
Concatenate them with the .operator:
将它们与.运算符连接起来:
$result = $data1 . " " . $data2;
Or use string interpolation:
或者使用字符串插值:
$result = "$data1 $data2";
回答by Why 50 points to comment...
$result = implode(' ', array($data1, $data2));
is more generic.
更通用。
回答by Tim G
Another form available is this:
另一种可用的形式是这样的:
<?php
$data1 = "the color is";
$data2 = "red";
$result = "{$data1} {$data2}";
回答by user710502
You can try the following line of code
您可以尝试以下代码行
$result = $data1." ".$data2;
回答by sunil
You can do this using PHP:
您可以使用 PHP 执行此操作:
$txt1 = "the color is";
$txt2 = " red!";
echo $txt1.$txt2;
This will combine two strings and the putput will be: "the color is red!".
这将组合两个字符串,输出将是:“颜色是红色!”。
回答by Farhan
$result = $data1 . $data2;
try this
尝试这个
回答by keyur sojitra
$s = "my name is ";
$s .= "keyur";
echo $s;
result:
结果:
my name is keyur

