Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg

PHP : Language Reference : Operators : String Operators

String Operators

There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side. Please read Assignment Operators for more information.

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

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

See also the manual sections on the String type and String functions.

Code Examples / Notes » language.operators.string


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>";
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.


I ran the follow script and found that using "$var" was 'mostly' slower than using ' '.$var
$var = 1;
for( $x=0; $x < 101; $x++ )
echo '<br /><br />var = int( '.$var.' )<br />';

for( $i=0; $i<100001; $i++ )
$string = " {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var} {$var}";
unset( $string );
echo 'One string with 15 $vars was set using one concat 100000 times and took '.$timer->fetch_time().' seconds to execute <br />';

for( $i=0; $i<100001; $i++ )
$string = ' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var.' '.$var;
unset( $string );
echo 'One string with 15 instances of $var was set using multiple concats 100000 times and took '.$timer->fetch_time().' seconds to execute';
Replacing $timer with a generic timing class of course.

php dot net

Also see for usage of  here doc syntax ("<<<")

anders dot benke

A word of caution - the dot operator has the same precedence as + and -, which can yield unexpected results.
$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:
$var = 3;
echo "Result: " . ($var + 3);

stephen clay

"{$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.

Change Language

Follow Navioo On Twitter
Operator Precedence
Arithmetic Operators
Assignment Operators
Bitwise Operators
Comparison Operators
Error Control Operators
Execution Operators
Incrementing/Decrementing Operators
Logical Operators
String Operators
Array Operators
Type Operators
eXTReMe Tracker