Strings are text
A string is a sequence of characters: a word, a sentence, a whole HTML page. Most of what a PHP website does is build strings and send them to the browser, so they are worth knowing well.
PHP has two everyday ways to write a string: in single quotes or in double quotes. They behave differently.
Single quotes: exactly as written
Text in single quotes is taken literally. The only special sequences are \' for a single quote and \\ for a backslash:
<?php
echo 'Price: $price' . "\n";
echo 'It\'s on sale' . "\n";
echo 'A line break looks like \n here' . "\n";Price: $price
It's on sale
A line break looks like \n hereDouble quotes: interpolation and escapes
In double quotes, PHP replaces variables with their values (interpolation) and understands escape sequences such as \n (new line), \t (tab), \" (a double quote) and \$ (a literal dollar sign):
<?php
$product = "Water Bottle";
$price = 12;
echo "The $product costs $price.\n";
echo "Name:\t$product\n";
echo "She said \"great value\" and paid \$$price.\n";The Water Bottle costs 12.
Name: Water Bottle
She said "great value" and paid $12.Which should you use? A common rule: single quotes for fixed text, double quotes when you insert variables or need \n. There is no meaningful speed difference in modern PHP, so choose for readability.
Joining strings with the dot
The concatenation operator is a dot. It joins two strings into one. Numbers are converted to text automatically:
<?php
$first = "Asha";
$last = "Verma";
$fullName = $first . " " . $last;
echo $fullName . "\n";
echo "Items: " . 3 . "\n";Asha Verma
Items: 3.= appends to the end of an existing string, which is useful when building output piece by piece:
<?php
$list = "Cart:";
$list .= " keyboard";
$list .= ", mug";
$list .= ", bottle";
echo $list . "\n";Cart: keyboard, mug, bottleIf you come from JavaScript, note that + does not join strings in PHP. "3" + "4" is the number 7, and "a" + "b" is a type error.
Braces for complex expressions
Simple variables interpolate on their own. For array elements with string keys, object properties of properties, or when text follows the variable directly, wrap the expression in braces:
<?php
$product = ['name' => 'Yoga Mat', 'price' => 24.99];
$size = 6;
echo "Product: {$product['name']} at {$product['price']}\n";
echo "Thickness: {$size}mm\n";Product: Yoga Mat at 24.99
Thickness: 6mmWithout the braces, "$sizemm" would look for a variable called $sizemm. The braces mark exactly where the variable ends. The rule to remember: inside double quotes, {$ starts an expression and } ends it.
Function calls cannot be interpolated. Use concatenation, or store the result in a variable first:
<?php
$name = "asha";
echo "Hello, " . ucfirst($name) . "!\n";Hello, Asha!Heredoc and nowdoc for longer text
For several lines of text, such as an email or a block of HTML, heredoc is cleaner than many concatenations. It starts with <<< and a label, and ends with the same label on its own line. It interpolates like double quotes:
<?php
$name = "Kavya";
$orderId = 1042;
$email = <<<TEXT
Hi $name,
Thank you for order #$orderId.
We will email you again when it ships.
TEXT;
echo $email . "\n";Hi Kavya,
Thank you for order #1042.
We will email you again when it ships.Since PHP 7.3 the closing label can be indented, and that indentation is removed from every line, so the text can line up with the surrounding code. Nowdoc uses quotes around the label, <<<'TEXT', and like single quotes does no interpolation. It is ideal for showing code samples.
Length and positions
A string's characters are numbered from 0. strlen() gives the length in bytes and square brackets read one character:
<?php
$code = "SAVE10";
echo strlen($code) . "\n";
echo $code[0] . "\n";
echo $code[-1] . "\n";6
S
0A negative index counts from the end. For text with accents or non-Latin characters, bytes and characters are not the same thing; the Unicode lesson explains why and which functions to use. Searching, replacing and slicing are in Searching, slicing and replacing text.
Common mistakes
- Using + to join strings. Use the dot.
- Expecting single quotes to interpolate.
'Hello $name'prints the dollar sign. - Writing "$item['name']" without braces. It is a parse error; write
"{$item['name']}". - Forgetting the semicolon after a heredoc's closing label, or putting other characters on that line.
Exercise
Given $city = "Pune" and $temp = 31, print the line Weather in Pune: 31°C (hot) twice: once with concatenation and once with interpolation. Use braces so °C is not read as part of the variable name.
Show one possible answer
<?php
$city = "Pune";
$temp = 31;
echo "Weather in " . $city . ": " . $temp . "°C (hot)\n";
echo "Weather in $city: {$temp}°C (hot)\n";Weather in Pune: 31°C (hot)
Weather in Pune: 31°C (hot)Key points
- Single quotes are literal; double quotes interpolate variables and understand escapes such as \n.
- The dot operator joins strings. .= appends to an existing string.
- Use braces for anything more than a simple variable inside double quotes: {$product['name']} or {$order->total}.
- Heredoc (<<<TEXT) is a multi-line double-quoted string; nowdoc (<<<'TEXT') is the single-quoted version.