Read Time:5 minsLanguages:url-selector#selectionChanged" data-controller="url-selector">EnglishDeutschEspañol
String concatenation is a very common & useful operation in PHP. There are many ways in which you might want to lớn join strings together like prepending, appending, or combining them at some other point.
Bạn đang xem: Php 8 concatenate string, variable and arrays examples
In this quick tip, I"ll teach you string concatenation in PHP. You"ll learn how to prepend or append strings in PHP & how lớn concatenate strings together at specific positions.
Prepend & 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.You can use the concatenation operator . If you want khổng lồ join strings and assign the result to lớn a third variable or output it. This is useful for both appending and prepending strings depending on their position.You can use the concatenating assignment operator .= if you want to lớn join the strings & assign the result to lớn 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 lớn append strings in PHP:
Here is an example that prepends different strings to a name in PHP:
Concatenating Strings at a Particular PointThere are two common cases when you need khổng lồ 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 khổng lồ 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 darkedeneurope.com, the value of the $index variable is simply the position at which we would lượt thích 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 lượt thích joining strings at a particular index. The only extra step we need to take is to lớn 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, và we need lớn 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 lớn take the length of words into account. Adding the word length to lớn our initial index gives us the correct position lớn concatenate the substring at the over of a particular word.
Final ThoughtsIn this quick tip, I have covered different scenarios for concatenating strings in PHP. You should now be able khổng lồ append, prepend, or join the strings at any other position you like.
The key khổng lồ joining the strings at a particular place is khổng lồ 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 lớn 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 và writing simple PHP loops và functions. Then you"ll build up khổng lồ 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 lớn practice responding to lớn GET và POST requests, parsing JSON, authenticating users, và using a MySQL database.