Skip to content

All topics  /  PHP

PHP 8 Concatenate String, Variable and Arrays Examples

PHP ·

Hi guys,

Today i will explained How to Concatenate String, Variable and Arrays in php 8 and other versions. THis articalis also working with php.

The concatenate term in PHP refers to joining multiple strings into one string; it also joins variables as well as the arrays.PHP, concatenation is done by using the concatenation operator (".") which is a dot.

Php is provided to the two types of Concatenation ("."),('.=') to provided in php 8.

So let's start to the artical.

(".") Operator


The dot concatenation operator in PHP concatenate its right and left arguments.

('.=') Operator

This second concatenation operator helps in appending the right side argument to the left side argument.

Concatenate Two Strings & Variables In PHP Example

<?php
    $string1 = 'John';
    $string2 = 'shina';
    $name_str = $string1 . ' ' . $string2;
    echo $name_str;
?>

Output

John shina

Concatenating Variable With String In PHP Example

<?php
    $var = "Hello";
    echo $var.' World';
?>

Output

Hello World

Concatenate Two Arrays In PHP Example

This example to learn how to merge two array in php using array_merge() method through.

<?php
    $myArr = array(1, "tv" => "ac", 2, "washing machine", 3);
    $mySecondArr = array("word", "abc", "vegetable" => "tomato", "state" => "washington dc");
    $output = array_merge($myArr, $mySecondArr);
    print_r($output);
?>

Output

Array
(
    [0] => 1
    [tv] => ac
    [1] => 2
    [2] => washing machine
    [3] => 3
    [4] => word
    [5] => abc
    [vegetable] => tomato
    [state] => washington dc
)

Now you can check your own.