116

Just wondering if there is something like .= for adding text to the beginning of a string, e.g.:

$foo =. 'bar';

which doesn't work.

Edit: example was originally $foo =. $bar; which can be achieved with $bar .= $foo;

yuttadhammo
  • 5,069
  • 6
  • 34
  • 45

5 Answers5

200

Nope. But you can do

$foo = "bar" . $foo
Eric V
  • 2,126
  • 1
  • 13
  • 5
4

I know this was asked/answered a while ago, but providing this answer as it is functionally equivalent despite it not being an assignment operator and no one commented on its usage for general string concatenation.

You may want to look into the use of the sprintf (documentation) family of functions for string concatenation. It provides a lot more sanitization and usability than just combining two strings 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 normally use vsprintf, since working with arrays is easier to manage value positions than individual arguments.

$vprepend = vsprintf('%2$s%1$s', array('foo', 'bar'));
var_dump($vprepend);
/* string(6) "barfoo" */

Also with an array of values, one can simply implode the resultant set of values for simple string concatenation.

 var_dump(implode('', array('bar', 'foo')));
 /* string(6) "barfoo" */
Will B.
  • 17,883
  • 4
  • 67
  • 69
  • 1
    Unfortunately, I do not think sprintf() is as fast as simple concats. And it certainly doesn't seem nearly as syntax-friendly, either. See: https://stackoverflow.com/q/7147305/2430549 – HoldOffHunger Apr 09 '18 at 17:23
  • Correct, though not sure how you mean by syntax friendly. While it is not as fast or simplistic as concatenation. `sprintf` is commonly used throughout multiple languages. `sprintf` has its place and purpose on when to use it. eg. casting of multiple values or enforcing a number of inputs using `sprintf`, provides you with more functionality than simple concatenation when is *required*. See: https://3v4l.org/8PWil But it should be not be used as a complete replacement for concatenation. – Will B. Apr 09 '18 at 22:46
4

You could always make your own function for that:

function prepend($string, $chunk) {
     if(!empty($chunk) && isset($chunk)) {
        return $string.$chunk;
     }
     else {
        return $string;
     }
}

$string would be the piece that you want to prepend and $chunk would be the text that you want something prepended onto it.

You could say the checks are optional, but by having that in there you don't need to worry about passing in a null value by accident.

Aaron
  • 10,386
  • 13
  • 37
  • 53
  • 1
    Thanks, you are right though. It's definitely not as easy or fast as something built in. Feel free to copy and paste if that helps make it easier :D – Aaron Aug 18 '11 at 18:35
  • 9
    If something is `!empty` it's also always `isset`, no need for this double check. Furthermore, since `$chunk` is a required parameter of the function, it *needs* to be passed to the function or PHP will complain. This means this check can be abbreviated to `if ($chunk)` (to see if it's *falsy* a.k.a. "empty"). But, if `$chunk` doesn't contain anything, nothing will happen anyway. So this whole function body can be abbreviated to `return $string . $chunk`. Which means this whole function is pretty unnecessary. – deceze Aug 19 '11 at 04:28
  • 1
    You can have a variable be set but also be empty. For example, a variable with the value of "" is empty but also set. – Aaron Aug 19 '11 at 16:02
  • 2
    Yes, but I'm saying your function is a very verbose way to write `$foo . $bar`. I challenge you to find any important case were your function does anything different. :) – deceze Aug 20 '11 at 07:40
3

You can wrap the built-in function substr_replace, where the arguments $start and $length can be set to 0, which prepends the $replacement to $string and returns the result, like so:

function prepend(& $string, $prefix) {
    $string = substr_replace($string, $prefix, 0, 0);
}

An example usage of the helper function would be:

$email_message = "Jonathan";
$appropriate_greeting = "Dear ";
prepend($email_message, $appropriate_greeting);
echo $email_message;

If you are into procedural programming, that is.

simon4ok
  • 43
  • 1
  • 7
2

Turning Blakethepatton's comment into an answer (thank you), a way to do this more neatly for longer variable names is by using a reference to the variable as follows:

$f = &$foo; $f = "bar{$f}";

This will save you typing over the answer by Eric V if your original variable name is 12 characters or longer.

An alternative on one line:

$f = 'bar' . ($f = &$foo);
robrecord
  • 504
  • 5
  • 15