How to Concatenate PHP Strings
Concatenation in PHP programming, is a process of joining two or more strings together by an operator through a PHP program. Operator can be called concatenator. It is possible to concatenate one string with another string in PHP server side scripting language. Even programmers often used it in programs to perform concatenation of two or more strings. In PHP programming, concatenation can be performed with two operators dot (.) and comma (,) symbols. Both concatenators join date of one string with date of another string.
Concatenation with dot (.) Operator
Dot operator is often and commonly used in PHP programming to concatenate data of one string with data of another string. It can be used as many times as you want. Data outputting function echo() cooperates to operation of concatenating strings with dot concatenator.
<?php echo "Data of a String"." and "."Data of another string"; ?>
Dot concatenator combined three different strings and made them a single string. The echo() function outputs them in web browser as a single string.
Data of a String and Data of another String
Similarly, print() function also welcomes the concatenation of two or more strings through a dot concatenator.
<?php print "Data of One String"." and "."Data of second string"; ?>
Dot operator groups the data of strings and print function outputs it in web browser by supporting to dot operator.
Data of One String and Data of second string
Strings are grouped by a dot as exampled in above php programs. In concatenation, programmer can insert a space to get some gap between strings but the space must be inserted inside the quotes.
According to above PHP code, both functions output data in web browser as per its functionality. In these programs; and term is written by giving a single character space before and after. For this reason, and term is clearly merged with first string and second string meaningfully with a space before and after. Even you can insert some space through quotes and concatenate it with strings to obtain space in web browser.
Concatenation with Comma (,) Operator
Concatenating strings can also be performed using comma operator. It returns same result but some functions do not support to this operation.
<?php echo "Data of a String"," and ","<br>","Data of another string"; ?>
Inserting comma between strings merges the data of strings properly, similar to concatenating the strings with dot but outputting function print() does not accept usage of comma to concatenate the data and return error in web browser. However, echo() function accepts it and returns same result. In this program, HTML break tag element is inserted between strings in concatenation to move some data of a merged string to next line. Otherwise, output data appears in browser as mixed data. Because of usage of break tag element, it avoided presence of third string with first and second strings in web browser but remember all three strings are already formed as a single string but some data is moved to next line because of break tag element.
Data of a String and
Data of another string
In this way, PHP programmers concatenate the data. You can use any method to concatenate the data of strings or variables using PHP programming as demonstrated above. Similarly, programmers can also insert html elements using concatenation methodology.