Строковые операторы

В PHP есть два оператора для работы со строками. Первый - оператор конкатенации ('.'), который возвращает объединение левого и правого аргумента. Второй - оператор присвоения с конкатенацией, который присоединяет правый аргумент к левому. Для получения более полной информации ознакомтесь с разделом Оператор присвоения.

<?php
$a
= "Hello ";
$b = $a . "World!"; // $b содержит строку "Hello World!"

$a = "Hello ";
$a .= "World!";    // $a содержит строку "Hello World!"
?>

Также ознакомьтесь с разделами документации Строки и Функции для работы со строками.



Строковые операторы
caliban at darklock dot com
29-Mar-2006 11:10
WRT Stephen's note:

My example of concatenation and array methods of string building does not include the interstitial logic, which is expected to include conditionals.

Concatenation method:

$str="This is my list";
if($list=="o") $str.="<ol>";
else $str.="<ul>";
foreach($item as $i) $str.="<li>$i</li>";
if($list=="o") $str.="</ol>";
else $str.="</ul>";

Array method:

$str=array("This is my list");
if($list=="o") $str[]="<ol>";
else $str[]="<ul>";
foreach($item as $i) $str[]="<li>$i</li>";
if($list=="o") $str[]="</ol>";
else $str[]="</ul>";
$str=implode("",$str);

You can't do either of these with a single double-quoted string. However, if what you are doing CAN be done in a single double-quoted string, Stephen is completely correct in observing that you should do that instead of concatenating.
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.
caliban at darklock dot com
15-Dec-2004 07:57
String concatenation is faster than the array method:

$str="";
$str.="Some string";
$str.="Some other string";
...
$str.="The last string";

That runs roughly twice as fast as:

$str=array();
$str[]="Some string";
$str[]="Some other string";
...
$str[]="The last string";
$str=implode("",$str);

Not that I think this is a terribly widespread practice, but I've got an awful lot of legacy code with this array method in it and a comment to the effect that it's faster than string concatenation. Testing has shown the exact opposite, so I figured I'd enlighten anyone else with this misconception.
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);
?>
php dot net at rinner dot at
19-Feb-2001 05:00
Also see manual/en/language.types.string.php for usage of  here doc syntax ("<<<")

<Логические операторыОператоры, работающие с массивами>
 Last updated: Tue, 15 Nov 2005