字符串运算符

有两个字符串(string)运算符。第一个是连接运算符("."),它返回其左右参数连接后的字符串。第二个是连接赋值运算符(".="),它将右边参数附加到左边的参数之后。更多信息见赋值运算符

<?php
$a 
"Hello ";
$b $a "World!"// now $b contains "Hello World!"

$a "Hello ";
$a .= "World!";     // now $a contains "Hello World!"
?>

User Contributed Notes

Joseph Alvini 27-Oct-2018 05:45
Concatenation inside of a for/foreach loop. Great for adding onto a string.

function Coffee($string){
    $arr = ["Loves", "Coffee", "With", "His", "Donuts"];
 
    foreach($arr as $items){
          $string  .= ' ' . $items;
    }
   
    return $string;
}

echo Coffee("Joe");
Rafael Serna 25-Sep-2018 08:37
Please note that concatenating an array item value using the  key => value syntax will result in a parse error if there are one or more spaces bewteen the concatenation operator (.) :

<?php

$elements
= [
   
'id' => 63,
   
'name' => '{"name": "'. str_repeat("a", 1000) .'"}' // <-- Parse error (there are spaces surrounding string_repeat)
];

$elements = [
   
'id' => 63,
   
'name' => '{"name": "'.str_repeat("a", 1000).'"}' // <-- OK
];
lci at live dot ca 26-Jan-2018 07:09
Note that the . operator accepts unquoted strings/undefined identifiers.

So $var = "test".test; will result in "testtest" being written to $var and not an error (as one might expect).

Be careful when trying to concatenate variables with strings, if you miss the $ before the variable name it will just concatenate the string with the variable name.

Example:

$variable1 = "testing";

$variable2 = "We are ".variable1;

$variable2 is now "We are variable1" as opposed to the intended "We are testing".
K.Alex 26-Dec-2012 10:22
As for me, curly braces serve good substitution for concatenation, and they are quicker to type and code looks cleaner. Remember to use double quotes (" ") as their content is parced by php, because in single quotes (' ') you'll get litaral name of variable provided:

<?php

 $a
= '12345';

// This works:
 
echo "qwe{$a}rty"; // qwe12345rty, using braces
 
echo "qwe" . $a . "rty"; // qwe12345rty, concatenation used

// Does not work:
 
echo 'qwe{$a}rty'; // qwe{$a}rty, single quotes are not parsed
 
echo "qwe$arty"; // qwe, because $a became $arty, which is undefined

?>
hexidecimalgadget at hotmail dot com 09-Feb-2009 09:37
If you attempt to add numbers with a concatenation operator, your result will be the result of those numbers as strings.

<?php

echo "thr"."ee";           //prints the string "three"
echo "twe" . "lve";        //prints the string "twelve"
echo 1 . 2;                //prints the string "12"
echo 1.2;                  //prints the number 1.2
echo 1+2;                  //prints the number 3

?>
mariusads::at::helpedia.com 27-Aug-2008 02:44
Be careful so that you don't type "." instead of ";" at the end of a line.

It took me more than 30 minutes to debug a long script because of something like this:

<?
echo 'a'.
$c = 'x';
echo 'b';
echo 'c';
?>

The output is "axbc", because of the dot on the first line.
Stephen Clay 23-Dec-2005 07:10
<?php
"{$str1}{$str2}{$str3}"; // one concat = fast
 
$str1. $str2. $str3;   // two concats = slow
?>
Use double quotes to concat more than two strings instead of multiple '.' operators.  PHP is forced to re-concatenate with every '.' operator.
anders dot benke at telia dot com 27-Apr-2004 09:53
A word of caution - the dot operator has the same precedence as + and -, which can yield unexpected results.

Example:

<php
$var = 3;

echo "Result: " . $var + 3;
?>

The above will print out "3" instead of "Result: 6", since first the string "Result3" is created and this is then added to 3 yielding 3, non-empty non-numeric strings being converted to 0.

To print "Result: 6", use parantheses to alter precedence:

<php
$var = 3;

echo "Result: " . ($var + 3);
?>
PHP8中文手册 站长在线 整理 版权归PHP文档组所有