How to combine two strings together in PHP?

glennanyway picture glennanyway · Dec 1, 2011 · Viewed 514.8k times · Source

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

For example:

$data1 = "the color is";
$data2 = "red";

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

Desired result:

$result = "the color is red";

Answer

Logan Serman picture Logan Serman · Dec 1, 2011
$result = $data1 . $data2;

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

$result = $data1 . ' ' . $data2;