How to Concatenate Strings in PHP

In PHP, strings are basically an array of characters.
This means that when you have two strings, you can concatenate them together to form a brand new string.
In this post, we'll learn how to concatenate strings in PHP.
How to concatenate strings in PHP
To learn how to concatenate strings, let's first create two strings.
$firstString = 'Hello';
$secondString = 'World';
From here, we can create a new string by concatenating them together using the .
operator.
$firstString = 'Hello';
$secondString = 'World';
$newString = $firstString . $secondString;
echo($newString);
HelloWorld
Keep in mind that because neither string has a space, the output will be HelloWorld
instead of Hello World
.
Alternatively, you could just concatenate the two strings with a separate space so the final string has a space:
$firstString = 'Hello';
$secondString = 'World';
$newString = $firstString . ' ' . $secondString;
echo($newString);
Hello World
Conclusion
In this post, we learned how to concatenate strings in PHP.
Simply use the .
operator to concatenate two strings together to form a new string.
Thanks for reading!
If you want to learn about web development, founding a start-up, bootstrapping a SaaS, and more, follow me on Twitter! You can also join the conversation over at our official Discord!
Leave us a message!