In PHP, concatenation is done by using the concatenation operator
(".") which is a dot. The php developers widely use the concatenation operator, it helps them to join string in their day to the day programming task.
PHP offers two types of string operators to deal with string concatenation.
| ||The dot concatenation operator in PHP concatenate its right and left arguments.|
| ||This second concatenation operator helps in appending the right side argument to the left side argument.|
You can combine two strings in PHP using the concatenate operator, check out the code example below:
<?php $string1 = 'John'; $string2 = 'Doe'; $name_str = $string1 . ' ' . $string2; echo $name_str; ?> // Result: John Doe
In this php tutorial, we will now learn to concatenate two variables, in the following example we have declared the two variables
$var2. Then in the
$JOIN_VAR variable we are adding the content of both the variables:
<?php $var_1 = "Tony Stark"; $var_2 = "MCU"; $JOIN_VAR = $var_1." ".$var_2." "; echo $JOIN_VAR; ?>
Output will be:
// Result: Tony Stark MCU
In this example we are Concatenating single variable with string:
<?php $var = "Hello"; echo $var.' World'; ?> // Result: Hello World
In this example, we will learn how to merge or join two arrays using the PHP array_merge() method. This method will allow us to combine 2 arrays data into one array.
<?php $myArr1 = array(1, "tv" => "ac", 2, "washing machine", 3); $myArr2 = array("word", "abc", "vegetable" => "tomato", "state" => "washington dc"); // Join arrays $output = array_merge($myArr1, $myArr2); print_r($output); ?>
Check out the result below:
Array (  => 1 [tv] => ac  => 2  => washing machine  => 3  => word  => abc [vegetable] => tomato [state] => washington dc )
Finally, we are done with PHP concatenating String, Variable and Arrays tutorials, I have tried to explain almost every possible scenario; however, If i have missed anything you can visit official PHP site to explore more.
Organizational hierarchy defines the roles and responsibilities of a person within an organization. Being a…
The keyboard helps users type whatever they want; in ionic apps, adding a keyboard is…
How do you show data on charts that change over time? If you don't know…
In this quick tutorial, we will learn how to create interval charts in React application.…
In this tutorial, you will learn how to display data in a histogram chart in…
What if? If you have to display the data based on a country, continent, region…