PHP › Strings & String Functions
What are the different ways to create and combine strings in PHP, and how does string concatenation using the dot operator work?
BeginnerAnswer
PHP lets you create strings using single quotes, double quotes, or the heredoc syntax for longer multiline text, and the dot operator lets you concatenate, meaning join together, two or more strings into one, which is commonly used to build up a larger piece of text from several smaller pieces, such as combining a greeting with a person's name that was stored in a separate variable.
$firstName = 'Sarah';
$greeting = 'Hello, ' . $firstName . '!';
echo $greeting;Real-world example A welcome email template concatenates a customer's first name together with several fixed pieces of greeting text to build a personalized message before sending it.