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
EasyCreate 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;
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types5Conditional Logic
If StatementIf - ElseThe Ternary OperatorNull Coalescing OperatorSwitch StatementRecap - Making Decisions3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators