Menu
Coddy logo textTech

Strings and Quotes

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

A string is simply a sequence of characters, like letters, numbers, symbols, or spaces. In PHP, you can create strings using either single quotes ('') or double quotes (""):

$message1 = 'Hello World!';
$message2 = "Welcome to PHP!";

Both approaches create valid strings, but there's an important difference between them.

Double quotes have a special feature called variable interpolation - they can include variables directly inside the string using the dollar $ sign:

<?php
$name = "Alice";
$greeting = "Hello, $name!";
echo $greeting; // Outputs: Hello, Alice!
?>

Single quotes, however, treat everything literally and don't process variables inside them. For example:

<?php
$name = "Alice";
$greeting = 'Hello, $name!';
echo $greeting; // Outputs: Hello, $name!
?>
challenge icon

Challenge

Easy

Create a variable called $greeting that uses double quotes to include the name variable directly inside the string in this exact format:
Welcome to PHP, [name]!

Cheat sheet

A string is a sequence of characters (letters, numbers, symbols, or spaces). In PHP, strings can be created using single quotes ('') or double quotes (""):

$message1 = 'Hello World!';
$message2 = "Welcome to PHP!";

Variable Interpolation: Double quotes allow you to include variables directly inside the string using the $ sign:

$name = "Alice";
$greeting = "Hello, $name!";
echo $greeting; // Outputs: Hello, Alice!

Single quotes treat everything literally and don't process variables:

$name = "Alice";
$greeting = 'Hello, $name!';
echo $greeting; // Outputs: Hello, $name!

Try it yourself

<?php
$name = "Bob";
$greeting = ?;

// Output the greeting
echo $greeting;
?>
quiz iconTest yourself

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

All lessons in Fundamentals