Any equivalent to .= for adding to beginning of string in PHP?

Php

Php Problem Overview


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;

Php Solutions


Solution 1 - Php

Nope. But you can do

$foo = "bar" . $foo

Solution 2 - Php

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.

Solution 3 - Php

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" */

Solution 4 - Php

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.

Solution 5 - Php

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);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyuttadhammoView Question on Stackoverflow
Solution 1 - PhpEric VView Answer on Stackoverflow
Solution 2 - PhpAaronView Answer on Stackoverflow
Solution 3 - PhpWill B.View Answer on Stackoverflow
Solution 4 - Phpsimon4okView Answer on Stackoverflow
Solution 5 - PhprobrecordView Answer on Stackoverflow