Search code examples
phpstringoperatorsconcatenationstring-concatenation

PHP left string concatenation operator?


This might be a very simple and easy question to answer, perhaps not.

To concatenate a string (by appending/right concat.) in php you use:

$a = "a";

$a .= "b";
//The value will be: "ab"

How do I do this, but the other way around?

Ej.:

$a = "a";

$a ???? "b";
//I NEED the value to be: "ba"

Solution

  • The closest thing to a built-in function would be using strpad like

    $a = str_pad($a, strlen($a)+strlen("b"), "b", STR_PAD_LEFT);
    

    but as others have pointed out its much simpler to just do $a = "b" . $a;