I know this was asked / answered some time ago, but provided this answer as it is functionally equivalent, despite the fact that it is not an assignment operator, and no one has commented on its use for general string concatenation.
You might want to check out the sprintf ( documentation ) family of functions for concatenating strings. This provides much more sanitation and usability than just combining two lines with assignment operators.
$foo = 'foo'; $append = sprintf('%1$s%2$s', $foo, 'bar'); var_dump($append); /* string(6) "foobar" */ $prepend = sprintf('%1$s%2$s', 'bar', $foo); var_dump($prepend); /* string(6) "barfoo" */ $prependInvert = sprintf('%2$s%1$s', $foo, 'bar'); var_dump($prependInvert); /* string(6) "barfoo" */ $wrap = sprintf('%2$s%1$s%2$s', $foo, 'bar'); var_dump($wrap); /* string(6) "barfoobar" */
I usually use vsprintf , since working with arrays is easier to control the positions of values ββthan separate arguments.
$vprepend = vsprintf('%2$s%1$s', array('foo', 'bar')); var_dump($vprepend);
Also with an array of values, just implode resulting set of values ββfor simple string concatenation.
var_dump(implode('', array('bar', 'foo')));
fyrye Feb 25 '15 at 15:07 2015-02-25 15:07
source share