01 · THE IDEAREAD → TRY → LEARN
Two ways to build a string
Most PHP programs assemble text from pieces: a name, a count, a list of tags. PHP gives you two main tools. Interpolation puts variables directly inside a double-quoted string (or a heredoc). Concatenation joins separate strings with the dot operator ., and .= appends to an existing string.
The key rule is that quotes decide what happens. Double-quoted strings expand variables and escape sequences such as \x41. Single-quoted strings do not: $name stays as the characters $, n, a, m, e. Nowdoc is the heredoc equivalent of single quotes, with no expansion at all.
Basic interpolation
When PHP sees a $ in a double-quoted string, it reads the characters that can form a variable name and substitutes the value. Run this example and compare the three lines.
$item = 'tea';
$count = 2;
echo "Order: $count $item", PHP_EOL;
echo "Order: {$count}x $item", PHP_EOL;
echo 'Order: $count $item', PHP_EOL;Order: 2 tea Order: 2x tea Order: $count $item
The second line uses braces. Without them, "$countx" would look for a variable called $countx, because x is a valid name character. Wrapping the expression as {$count} marks exactly where the name ends. This is the most common source of unexpected output, so use braces whenever text sits directly against a variable.
The older ${name} form is deprecated as of PHP 8.2. Use {$name} instead.
Array elements and properties
The simple syntax can read one array element or one property. For anything more complex, use the braced form.
$tags = ['php', 'web'];
$user = new stdClass();
$user->name = 'Ada';
$prices = ['tea' => 3];
echo "First tag: $tags[0]", PHP_EOL;
echo "User: $user->name", PHP_EOL;
echo "Tea costs {$prices['tea']} dollars", PHP_EOL;First tag: php User: Ada Tea costs 3 dollars
Inside braces you write the expression as normal PHP, including quoted keys such as $prices['tea']. That is clearer than relying on the unquoted-key shortcut in simple syntax.
Concatenation, escapes and literal dollars
Concatenation never expands anything inside the pieces you join, which makes it a good fit for values you do not control. If a user name contains $5, joining it with . leaves the text alone. Interpolation also does not re-scan a variable's contents; only the literal in your source code is parsed.
function label(string $name, int $count): string
{
return $name . ' (' . $count . ')';
}
$text = 'Total: ';
$text .= label('Tea', 4);
echo $text, PHP_EOL;
echo "Cost: \$5", PHP_EOL;
echo "Letter: \x41", PHP_EOL;
echo 'Letter: \x41', PHP_EOL;Total: Tea (4) Cost: $5 Letter: A Letter: \x41
To print a real dollar sign in a double-quoted string, escape it as \$, or use single quotes. Integers are converted to strings automatically when concatenated or interpolated.
Edge cases to remember
- A
$that cannot start a valid variable name stays literal, as in"cost $ 5". - An empty string or empty array produces no text, so decide explicitly how an empty list should look.
- Loops that append with
.=often leave a leading or trailing separator. Handle the first item or the empty case deliberately. - Single-quoted strings are easy to choose by accident, producing the literal text
$namein your output.
Worked plan for the exercise
You will write build_label(string $name, int $count, array $tags): string. The output has the form Name [count], followed by a space and #tag for each tag. For example, build_label('Book', 3, ['php', 'web']) returns Book [3] #php #web. With no tags the result is just Pen [0], with no trailing space. A name such as Cost $5 must come through unchanged.
Start with the base "{$name} [{$count}]", then loop over the tags and append ' #' . $tag to each. Putting the separator before each tag means the empty-list case needs no special code. Return the string; do not echo it, because the tests check returned values.
Recap
Double quotes expand variables; single quotes do not. Use braces to mark where an expression ends. Use . and .= to join pieces safely, and watch separators at the edges of lists.
Functions · Arrays · Types
- Build labels from values without unexpected interpolation
- Implement and test a deterministic PHP 8.4 function