Read Time:5 minsLanguages:url-selector#selectionChanged" data-controller="url-selector">EnglishDeutschEspañolString 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" /> Read Time:5 minsLanguages:url-selector#selectionChanged" data-controller="url-selector">EnglishDeutschEspañolString 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" />

Php

post-promotion-placement#checkSidebarAdDisplayCondition" data-controller="sticky-ad post-promotion-placement">
Read Time:5 minsLanguages:url-selector#selectionChanged" data-controller="url-selector">EnglishDeutschEspañol

String concatenation is a very comtháng & useful operation in PHPhường. There are many ways in which you might want to lớn join strings together lượt thích prepending, appending, or combining them at some other point.

Bạn đang xem: Php

In this quichồng tip, I"ll teach you string concatenation in PHPhường. You"ll learn how to lớn prepend or append strings in PHPhường & how to concatenate strings together at specific positions.

Prepover và Append Strings in PHP

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

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 & prepending strings depending on their position.You can use the concatenating assignment operator .= if you want to lớn join the strings và assign the result lớn the same variable. It is a shorter way of appending the argument on the right side khổng lồ the argument on the left side.

Here is an example which shows how khổng lồ appkết thúc strings in PHP:

Here is an example that prepends different strings khổng lồ a name in PHP:

Concatenating Strings at a Particular Point

There are two comtháng cases when you need to lớn join strings at some place other than the beginning or the kết thúc 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 lớn split the main string in two parts at the desired index và then join the parts back together with our string in the middle.

In the above sầu obatambeienwasirherbal.com, the value of the $index variable is simply the position at which we would lượt thích to lớn 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 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 bloông chồng or $haystachồng.

Xem thêm: Hướng Dẫn Các Bước Sửa Lỗi Burn In, Hiện Tượng Lưu Ảnh Màn Hình

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 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 lớn concatenate the substring at the beginning of a particular word.

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

Final Thoughts

In this quiông xã tip, I have sầu covered different scenargame ios for concatenating strings in PHP. You should now be able lớn appover, prepover, 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 lớn concatenate strings before or after a particular word.

Learn PHPhường. With a Free Online Course

If you want khổng lồ learn PHPhường, kiểm tra out our không tính phí online course on PHP fundamentals!


*


In this course, you"ll learn the fundamentals of PHPhường programming. You"ll start with the basics, learning how PHPhường works and writing simple PHPhường loops & functions. Then you"ll build up to lớn 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 lớn GET & POST requests, parsing JSON, authenticating users, and using a MySQL database.