How can I combine two strings together in PHP?

I don’t actually know how to describe what I wanted, but I’ll show you:

For example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$data1 = "the color is";
$data2 = "red";
</code>
<code>$data1 = "the color is"; $data2 = "red"; </code>
$data1 = "the color is";
$data2 = "red";

What should I do (or process) so $result is the combination of $data1 and $data2?

Desired result:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = "the color is red";
</code>
<code>$result = "the color is red"; </code>
$result = "the color is red";

0

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = $data1 . $data2;
</code>
<code>$result = $data1 . $data2; </code>
$result = $data1 . $data2;

This is called string concatenation. Your example lacks a space though, so for that specifically, you would need:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = $data1 . ' ' . $data2;
</code>
<code>$result = $data1 . ' ' . $data2; </code>
$result = $data1 . ' ' . $data2;

0

There are several ways to concatenate two strings together.

Use the concatenation operator . (and .=)

In PHP . is the concatenation operator which returns the concatenation of its right and left arguments

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$data1 = "the color is";
$data2 = "red";
$result = $data1 . ' ' . $data2;
</code>
<code>$data1 = "the color is"; $data2 = "red"; $result = $data1 . ' ' . $data2; </code>
$data1 = "the color is";
$data2 = "red";
$result = $data1 . ' ' . $data2;

If you want to append a string to another string you would use the .= operator:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$data1 = "the color is ";
$data1 .= "red"
</code>
<code>$data1 = "the color is "; $data1 .= "red" </code>
$data1 = "the color is ";
$data1 .= "red"

Complex (curly) syntax / double quotes strings

In PHP variables contained in double quoted strings are interpolated (i.e. their values are “swapped out” for the variable). This means you can place the variables in place of the strings and just put a space in between them. The curly braces make it clear where the variables are.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = "{$data1} {$data2}";
</code>
<code>$result = "{$data1} {$data2}"; </code>
$result = "{$data1} {$data2}";

Note: this will also work without the braces in your case:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = "$data1 $data2";
</code>
<code>$result = "$data1 $data2"; </code>
$result = "$data1 $data2";

You can also concatenate array values inside a string :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$arr1 = ['val' => 'This is a'];
$arr2 = ['val' => 'test'];
$variable = "{$arr1['val']} {$arr2['val']}";
</code>
<code>$arr1 = ['val' => 'This is a']; $arr2 = ['val' => 'test']; $variable = "{$arr1['val']} {$arr2['val']}"; </code>
$arr1 = ['val' => 'This is a'];
$arr2 = ['val' => 'test'];
$variable = "{$arr1['val']} {$arr2['val']}";

Use sprintf() or printf()

sprintf() allows us to format strings using powerful formatting options. It is overkill for such simple concatenation but it handy when you have a complex string and/or want to do some formatting of the data as well.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = sprintf("%s %s", $data1, $data2);
</code>
<code>$result = sprintf("%s %s", $data1, $data2); </code>
$result = sprintf("%s %s", $data1, $data2);

printf() does the same thing but will immediately display the output.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>printf("%s %s", $data1, $data2);
// same as
$result = sprintf("%s %s", $data1, $data2);
echo $result;
</code>
<code>printf("%s %s", $data1, $data2); // same as $result = sprintf("%s %s", $data1, $data2); echo $result; </code>
printf("%s %s", $data1, $data2);
// same as
$result = sprintf("%s %s", $data1, $data2);
echo $result;

Heredoc

Heredocs can also be used to combine variables into a string.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result= <<<EOT
$data1 $data2
EOT;
</code>
<code>$result= <<<EOT $data1 $data2 EOT; </code>
$result= <<<EOT
$data1 $data2
EOT;

Use a , with echo()

This only works when echoing out content and not assigning to a variable. But you can use a comma to separate a list of expressions for PHP to echo out and use a string with one blank space as one of those expressions:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>echo $data1, ' ', $data2;
</code>
<code>echo $data1, ' ', $data2; </code>
echo $data1, ' ', $data2;

0

Another possibility is the .= operator. I’m using it for huge SQL queries.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$string = "the color is ";
$string .= "red";
echo $string; // Gives: the color is red
</code>
<code>$string = "the color is "; $string .= "red"; echo $string; // Gives: the color is red </code>
$string = "the color is ";
$string .= "red";

echo $string; // Gives: the color is red

3

Concatenate them with the . operator:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = $data1 . " " . $data2;
</code>
<code>$result = $data1 . " " . $data2; </code>
$result = $data1 . " " . $data2;

Or use string interpolation:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = "$data1 $data2";
</code>
<code>$result = "$data1 $data2"; </code>
$result = "$data1 $data2";

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = implode(' ', array($data1, $data2));
</code>
<code>$result = implode(' ', array($data1, $data2)); </code>
$result = implode(' ', array($data1, $data2));

is more generic.

2

Another form available is this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?php
$data1 = "the color is";
$data2 = "red";
$result = "{$data1} {$data2}";
</code>
<code><?php $data1 = "the color is"; $data2 = "red"; $result = "{$data1} {$data2}"; </code>
<?php
$data1 = "the color is";
$data2 = "red";
$result = "{$data1} {$data2}";

You can try the following line of code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = $data1 . " " . $data2;
</code>
<code>$result = $data1 . " " . $data2; </code>
$result = $data1 . " " . $data2;

1

You can do this using PHP:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$txt1 = "the color is";
$txt2 = " red!";
echo $txt1 . $txt2;
</code>
<code>$txt1 = "the color is"; $txt2 = " red!"; echo $txt1 . $txt2; </code>
$txt1 = "the color is";
$txt2 = " red!";
echo $txt1 . $txt2;

This will combine two strings and the output will be: “the color is red!”.

3

A period is used to concatenate strings. Simple example to turn two string variables into a single variable:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$full = $part1 . $part2;
</code>
<code>$full = $part1 . $part2; </code>
$full = $part1 . $part2;

In your example, you’d want to do:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = $data1 . ' ' . $data2;
</code>
<code>$result = $data1 . ' ' . $data2; </code>
$result = $data1 . ' ' . $data2;

You’ll notice I added a string of one space between the two variables. This is because your original $data1 did not end with a space. If you had combined them without it, your $result variable would end up looking like the color isred.

This should work for you:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = $data1 . " " . $data2;`
</code>
<code>$result = $data1 . " " . $data2;` </code>
$result = $data1 . " " . $data2;` 

Reference: PHP String Variables

Try this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = $data1 . $data2;
</code>
<code>$result = $data1 . $data2; </code>
$result = $data1 . $data2;

1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$s = "my name is ";
$s .= "keyur";
echo $s;
</code>
<code>$s = "my name is "; $s .= "keyur"; echo $s; </code>
$s = "my name is ";

$s .= "keyur";

echo $s; 

result:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>my name is keyur
</code>
<code>my name is keyur </code>
my name is keyur

2

I am not exactly clear with what your requirements are, but basically you could separately define the two variables and thereafter combine them together.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$data1="The colour is ";
$data2="red";
$result=$data1.$data2;
</code>
<code>$data1="The colour is "; $data2="red"; $result=$data1.$data2; </code>
$data1="The colour is ";
$data2="red";

$result=$data1.$data2;

By doing so you can even declare $data2 as a global level, so you could change its value during execution. For instance, it could obtain the answer “red” from a checkbox.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = $data1 . ' ' . $data2;
</code>
<code>$result = $data1 . ' ' . $data2; </code>
$result = $data1 . ' ' . $data2;

or

The best way.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = "$data1 $data2";
</code>
<code>$result = "$data1 $data2"; </code>
$result = "$data1 $data2";

or

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = "{$data1} {$data2};
</code>
<code>$result = "{$data1} {$data2}; </code>
$result = "{$data1} {$data2};

The easiest way that already mentioned here:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = $data1 . ' ' . $data2;
</code>
<code>$result = $data1 . ' ' . $data2; </code>
$result = $data1 . ' ' . $data2;

In some cases you may need to use functions like join() or implode(). Both the functions does the same thing that is it convert array items into string.

Example 1:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = join(' ', [$data1, $data2]);
</code>
<code>$result = join(' ', [$data1, $data2]); </code>
$result = join(' ', [$data1, $data2]);

Example 2:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = implode(' ', [$data1, $data2]);
</code>
<code>$result = implode(' ', [$data1, $data2]); </code>
$result = implode(' ', [$data1, $data2]);

you can make your on array have empty not empty values; and use filter and implode to make full sentence

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$str1 = 'hello';
$str2 = 'my name';
$str3 = 'vijay';
$str4 = '';
$fullsentence = array_filter([$str1, $str2, $str3, $str4]);
echo implode(' ', $fullsentence);
</code>
<code>$str1 = 'hello'; $str2 = 'my name'; $str3 = 'vijay'; $str4 = ''; $fullsentence = array_filter([$str1, $str2, $str3, $str4]); echo implode(' ', $fullsentence); </code>
$str1 = 'hello';
$str2 = 'my name';
$str3 = 'vijay';
$str4 = '';
$fullsentence = array_filter([$str1, $str2, $str3, $str4]);
echo implode(' ', $fullsentence);

  1. Concatenate the string (space between each string)

    Code Snippet:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code><?php
    $txt1 = "Sachin";
    $txt2 = "Tendulkar";
    $result = $txt1 . $txt2 ;
    echo $result . "n";
    ?>
    </code>
    <code><?php $txt1 = "Sachin"; $txt2 = "Tendulkar"; $result = $txt1 . $txt2 ; echo $result . "n"; ?> </code>
    <?php
        $txt1 = "Sachin";
        $txt2 = "Tendulkar";
    
        $result = $txt1 . $txt2 ;
        echo $result . "n";
    ?>
    

    Output: SachinTendulkar

  2. Concatenate the string where space exists

    Code Snippet:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code> <?php
    $txt1 = "Sachin";
    $txt2 = "Tendulkar";
    $result = $txt1 . " " . $txt2;
    echo $result . "n";
    ?>
    </code>
    <code> <?php $txt1 = "Sachin"; $txt2 = "Tendulkar"; $result = $txt1 . " " . $txt2; echo $result . "n"; ?> </code>
     <?php
         $txt1 = "Sachin";
         $txt2 = "Tendulkar";
    
         $result = $txt1 . " " . $txt2;
         echo $result . "n";
     ?>
    

    Output: Sachin Tendulkar

  3. Concatenate the string using printf function.

    Code Snippet:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code> <?php
    $data1 = "Sachin";
    $data2 = "Tendulkar";
    printf("%s%sn", $data1, $data2);
    printf("%s %sn", $data1, $data2);
    ?>
    </code>
    <code> <?php $data1 = "Sachin"; $data2 = "Tendulkar"; printf("%s%sn", $data1, $data2); printf("%s %sn", $data1, $data2); ?> </code>
     <?php
         $data1 = "Sachin";
         $data2 = "Tendulkar";
         printf("%s%sn", $data1, $data2);
         printf("%s %sn", $data1, $data2);
     ?>
    

    Output:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code> SachinTendulkar
    Sachin Tendulkar
    </code>
    <code> SachinTendulkar Sachin Tendulkar </code>
     SachinTendulkar
     Sachin Tendulkar
    

1

Combine two strings together in PHP:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$result = $data1 . ' ' . $data2;
</code>
<code>$result = $data1 . ' ' . $data2; </code>
$result = $data1 . ' ' . $data2;

1

Try this. We can append a string in PHP with the dot (.) symbol

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$data1 = "the color is";
$data2 = "red";
echo $data1 . $data2; // The color isred
echo $data1 . " " . $data2; // The color is red (we have appended space)
</code>
<code>$data1 = "the color is"; $data2 = "red"; echo $data1 . $data2; // The color isred echo $data1 . " " . $data2; // The color is red (we have appended space) </code>
$data1 = "the color is";
$data2 = "red";

echo $data1 . $data2; // The color isred
echo $data1 . " " . $data2; // The color is red (we have appended space)

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật