PHP Strings
A string is a sequence of characters wrapped in single or double quotes.
Syntax
$str = "Hello" . " " . "World";A string is any text inside quotes. PHP supports both single quotes '...' and double quotes "...".
The concatenation operator
Use the dot . operator to join two strings together.
Example
Loading editor…
Press Run to execute the code.
When to use it
- Store a user's full name as a string variable and display it in a welcome message.
- Wrap SQL query fragments in single-quoted strings to avoid variable interpolation issues.
- Build an HTML email body by concatenating string segments with variable content.
More examples
Single vs double quotes
Single-quoted strings are literal; double-quoted strings interpolate variables and escape sequences.
<?php
$name = 'World';
$single = 'Hello, $name!'; // Hello, $name!
$double = "Hello, $name!"; // Hello, World!
echo $single . "\n";
echo $double;Heredoc for multi-line strings
Heredoc syntax handles multi-line strings with interpolation, keeping HTML templates readable.
<?php
$user = 'Alice';
$score = 42;
$html = <<<HTML
<p>Player: $user</p>
<p>Score: $score</p>
HTML;
echo $html;String concatenation operator
The dot operator concatenates strings; .= appends to an existing string variable in place.
<?php
$first = 'Hello';
$last = 'World';
$greeting = $first . ', ' . $last . '!';
$greeting .= ' Have a great day.';
echo $greeting;
Discussion