iDevie
September 2021
M T W T F S S
« Aug    
 12345
6789101112
13141516171819
20212223242526
27282930  

Categories


String concatenation is a very common and useful operation in PHP. There are many ways in which you might want to join strings together like prepending, appending, or combining them at some other point.

In this quick tip, I’ll teach you string concatenation in PHP. You’ll learn how to prepend or append strings in PHP and how to concatenate strings together at specific positions.

Prepend and Append Strings in PHP

Concatenating strings in PHP by either appending or prepending is actually pretty simple. There are two string operators in PHP meant for this specific purpose.

  1. You can use the concatenation operator . if you want to join strings and assign the result to a third variable or output it. This is useful for both appending and prepending strings depending on their position.
  2. You can use the concatenating assignment operator .= if you want to join the strings and assign the result to the same variable. It is a shorter way of appending the argument on the right side to the argument on the left side.

Here is an example which shows how to append strings in PHP:

Here is an example that prepends different strings to a name in PHP:

Concatenating Strings at a Particular Point

There are two common cases when you need to join strings at some place other than the beginning or the end of a string. These are splitting the main string at a particular index, or splitting the main string after a particular word. We will cover both these situations in this section.

Concatenating Strings at a Particular Index

We can use the substr() function in PHP to split the main string in two parts at the desired index and then join the parts back together with our string in the middle.

In the above code, the value of the $index variable is simply the position at which we would like to concatenate our substring inside the main string.

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.


Comments 0
There are currently no comments.