Menu
Coddy logo textTech

String Concatenation

Part of the Fundamentals section of Coddy's PHP journey — lesson 12 of 71.

String concatenation is the process of joining two or more strings to create a single, longer string.

Use the dot operator (.) to concatenate strings. This operator takes the strings on its left and right sides and combines them into one continuous string:

$firstName = "John";
$lastName = "Smith";
$fullName = $firstName . $lastName;
echo $fullName; // Outputs: JohnSmith

Notice that concatenation doesn't automatically add spaces between strings. If you want spaces or other characters between your strings, you need to include them explicitly:

$fullName = $firstName . " " . $lastName;
echo $fullName; // Outputs: John Smith
challenge icon

Challenge

Easy

You are provided with a first name and a last name variables.

Create two string variables using concatenation:

  • $fullName - concatenate first name, a space, and last name
  • $greeting - concatenate Hello,  with the full name

Cheat sheet

String concatenation joins two or more strings together using the dot operator (.):

$firstName = "John";
$lastName = "Smith";
$fullName = $firstName . $lastName;
echo $fullName; // Outputs: JohnSmith

Concatenation doesn't add spaces automatically. Include them explicitly:

$fullName = $firstName . " " . $lastName;
echo $fullName; // Outputs: John Smith

Try it yourself

<?php
$firstName = "Jhon";
$lastName = "Smith";

// Write your code below
$fullName = ?;
$greeting = ?;

echo $fullName . "\n";
echo $greeting . "\n";

?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals