In this PHP tutorial, you shall learn how to prepend a string to the given string using String Concatenation Operator, with example programs.

PHP – Prepend String

To prepend string with another string, in PHP, use string concatenation operator. Provide the other string as left operand and the given string as right operand to concatenation operator, and assign the result the variable in which the given string is stored.

The syntax to prepend a string $string2 to given string $string1 is

$string1 = $string2 . $string1

Examples

ADVERTISEMENT

1. Prepend string “banana” to “apple”

In this example, we will take a string in $string1 variable, and prepend another string $string2 to $string1.

PHP Program

<?php
$string1 = "apple";
$string2 = "banana";
$string1 = $string2 . $string1;
echo $string1;
?>

Output

PHP - Prepend String

Conclusion

In this PHP Tutorial, we have learned how to prepend string with another string using string concatenation operator, and covered example program for this scenario.