PHP Strings
A string is a sequence of characters such as "Hello", 'order #42' or an entire HTML page. Because PHP is mostly used to build text that a browser displays, strings are the data type you will work with more than any other.
Single quotes vs double quotes
PHP has two everyday ways to write a string literal, and they behave differently:
| Literal | Variables expanded? | Escape sequences | Example |
|---|---|---|---|
'single' |
No | Only \' and \\ |
'Total: $sum\n' stays exactly as typed |
"double" |
Yes | \n, \t, \$, \", \\, \u{1F418} … |
"Total: $sum\n" inserts the value and a newline |
<?php
$fruit = "mango";
echo 'I like $fruit\n', PHP_EOL;
echo "I like $fruit\n";
echo "Tab\tseparated\tcolumns\n";
echo "A literal dollar sign: \$fruit\n";
echo 'It\'s a single-quoted string', "\n";I like $fruit\n
I like mango
Tab separated columns
A literal dollar sign: $fruit
It's a single-quoted string
Tip: Use single quotes when the text is fixed and contains no variables — what you see is exactly what you get. Switch to double quotes when you want interpolation or
\n.
Interpolation: putting variables inside strings
Inside double quotes PHP replaces simple variables with their values. For anything more complex — array keys with quotes, object properties chained together, or a variable directly followed by letters — wrap the expression in curly braces:
<?php
$item = "pencil";
$count = 3;
$price = ["pencil" => 0.75];
$user = new stdClass();
$user->name = "Mai";
echo "You bought $count $item.\n";
echo "Plural: {$item}s\n"; // without braces PHP looks for $items
echo "Unit price: {$price['pencil']}\n";
echo "Customer: {$user->name}\n";You bought 3 pencil.
Plural: pencils
Unit price: 0.75
Customer: Mai
Joining strings with the dot operator
PHP uses . (not +) to concatenate strings, and .= to append to an existing variable. The + operator is reserved for numbers.
<?php
$first = "Ada";
$last = "Lovelace";
$full = $first . " " . $last;
echo $full . "\n";
$log = "start";
$log .= " -> load";
$log .= " -> done";
echo $log . "\n";
echo "Length of full name: " . strlen($full) . "\n";Ada Lovelace
start -> load -> done
Length of full name: 12
Heredoc and nowdoc
For multi-line text, PHP offers two block syntaxes. Heredoc (<<<LABEL) behaves like a double-quoted string, nowdoc (<<<'LABEL') like a single-quoted one. Since PHP 7.3 the closing label may be indented, and that indentation is removed from every line.
<?php
$name = "Linh";
$total = 18.5;
$receipt = <<<TEXT
Customer: $name
Total: {$total} USD
Thank you!
TEXT;
echo $receipt, "\n";
$template = <<<'RAW'
Variables like $name are NOT replaced here.
RAW;
echo $template, "\n";Customer: Linh
Total: 18.5 USD
Thank you!
Variables like $name are NOT replaced here.
Accessing single characters
A string can be indexed like an array, starting at 0. Negative offsets count from the end (-1 is the last character).
<?php
$word = "elephant";
echo $word[0], "\n";
echo $word[3], "\n";
echo $word[-1], "\n";
echo strlen($word), " characters\n";e
p
t
8 characters
Note:
strlen()counts bytes, not characters. For text with accents or emoji (UTF-8), usemb_strlen()— for examplestrlen("café")is 5 butmb_strlen("café")is 4.
Summary
| Task | Syntax |
|---|---|
| Literal text | 'text' |
| Text with variables / escapes | "Hello $name\n" |
| Complex expression in a string | "{$arr['key']}", "{$obj->prop}" |
| Concatenate | $a . $b |
| Append | $a .= $b |
| Multi-line with variables | heredoc <<<EOT |
| Multi-line raw | nowdoc <<<'EOT' |
Test yourself
What does echo 'Hi $name'; print when $name = "Bo"?
- Hi Bo
- Hi $name
- Hi
- An error
Which operator joins two strings in PHP?
+&.,
Which heredoc-style syntax does NOT expand variables?
<<<EOT<<<"EOT"<<<'EOT'(nowdoc)- All of them expand variables