PHP 8 Concatenate String, Variable and Arrays Examples

In this tutorial, we are going to learn how to concatenate String, Variable and Arrays in PHP. The concatenate term in PHP refers to joining multiple strings into one string; it also joins variables as well as the arrays.

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.

Understand PHP Concatenate Operator

PHP offers two types of string operators to deal with string concatenation.

Operator Detail
(".") 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.

Concatenate Two Strings in PHP

You can combine two strings in PHP using the concatenate operator, check out the code example below:

  $string1 = 'John';
  $string2 = 'Doe';
  $name_str = $string1 . ' ' . $string2;
  echo $name_str;

// Result: John Doe

Concatenate Two Variables in PHP

In this php tutorial, we will now learn to concatenate two variables, in the following example we have declared the two variables $var1 and $var2. Then in the $JOIN_VAR variable we are adding the content of both the variables:

    $var_1 =  "Tony Stark";
    $var_2 = "MCU";
    $JOIN_VAR = $var_1." ".$var_2." ";
    echo $JOIN_VAR;

Output will be:

// Result: Tony Stark MCU 

Concatenating variable with string in PHP

In this example we are Concatenating single variable with string:

  $var = "Hello";
  echo $var.' World';

// Result: Hello World

Concatenate Two Arrays in PHP

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.

   $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);

Check out the result below:

    [0] => 1
    [tv] => ac
    [1] => 2
    [2] => washing machine
    [3] => 3
    [4] => word
    [5] => 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.


I am Digamber, a full-stack developer and fitness aficionado. I created this site to bestow my coding experience with newbie programmers. I love to write on JavaScript, ECMAScript, React, Angular, Vue, Laravel.

Recent Posts

How to Create and Use Redux Store in React Js

In this tutorial, we will learn how to create a Redux Store in React application.…

2 days ago

How to Build Dynamic Load More Feature in React Js

This detailed guide will cover how to create the Load More button and display data…

5 days ago

How to Integrate Redux Persist to React Redux Store

In this tutorial, we will step by step learn how to configure redux persist in…

1 week ago

React Redux Save Data in Local Storage with Persist Tutorial

In this comprehensive tutorial, we will learn how to persist redux store's states in React…

2 weeks ago

React Js Tailwind CSS Add Light and Dark Mode Toggler Tutorial

In this guide, we are going to learn how to add dark mode and light…

2 weeks ago

React RTK Query Delete Data with Api Slice Tutorial

This extensive guide will teach you how to get and delete data objects from the…

2 weeks ago