How to Concatenate Strings in PHP
Table of Contents
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.
PHP$firstString = 'Hello';
$secondString = 'World';
From here, we can create a new string by concatenating them together using the .
operator.
PHP$firstString = 'Hello';
$secondString = 'World';
$newString = $firstString . $secondString;
echo($newString);
BASHHelloWorld
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:
PHP$firstString = 'Hello';
$secondString = 'World';
$newString = $firstString . ' ' . $secondString;
echo($newString);
BASHHello 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!
- Getting Started with TypeScript
- How to Install Node on Windows, macOS and Linux
- Best Visual Studio Code Extensions for 2022
- How to deploy a PHP app using Docker
- Getting Started with Deno
- How to deploy a MySQL Server using Docker
- How to deploy a Node app using Docker
- Learn how to use v-model with a custom Vue component
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Using Push.js to Display Web Browser Notifications
- Getting Started with React
- Getting Started with Vuex: Managing State in Vue