How to prepend a string in PHP

There’s no exact function to prepend a string in PHP. But PHP concatenation operator (.) is used to returns the concatenation of its left and right arguments.

There are two string operators :

  • Concatenation operator (‘.’) – Concatenation of str1 and str2

Example: $str1 . $str2

  • Concatenating assignment operator (‘.=’) – Appends the str2 to the str1

Example:  $str1 .= $str2

<?php
$str1 = "PHP";
$str2 = "Tutorials!";
$str2 = $str1 . " " . $str2;
echo $str2; // Outputs: PHP Tutorials!
?>

 

 

 

Praveen Kumar

I am a fullstack web developer with a passion for SEO, creating stunning websites, and organic marketing. I am a perfectionist.

Post navigation