### Concatenating Strings Before or After a Particular Word

Concatenating a string before or after a particular word works just like joining strings at a particular index. The only extra step we need to take is to figure out the index. The strpos($haystack,$needle) function is perfect for this purpose. It simply finds the position of our $needle string inside the main text block or $haystack.

Here is an example of concatenating strings before a particular word.

The big difference here is that we have moved the \$index variable inside our loop. This is because the position of our word will probably be different in each string, and we need to adjust the value of the index accordingly.

The strpos() function gives us the position of the first character of our substring inside the main string. This makes it easier for us to concatenate the substring at the beginning of a particular word.

When adding substrings at the end of words, we also need to take the length of words into account. Adding the word length to our initial index gives us the correct position to concatenate the substring at the end of a particular word.

## Final Thoughts

In this quick tip, I have covered different scenarios for concatenating strings in PHP. You should now be able to append, prepend, or join the strings at any other position you like.

The key to joining the strings at a particular place is to simply find the index. This is exactly what we did when we wanted to concatenate strings before or after a particular word.

## Learn PHP With a Free Online Course

If you want to learn PHP, check out our free online course on PHP fundamentals!

In this course, you'll learn the fundamentals of PHP programming. You'll start with the basics, learning how PHP works and writing simple PHP loops and functions. Then you'll build up to coding classes for simple object-oriented programming (OOP). Along the way, you'll learn all the most important skills for writing apps for the web: you'll get a chance to practice responding to GET and POST requests, parsing JSON, authenticating users, and using a MySQL database.