Single quotes, double quotes and interpolation
A PHP string is a sequence of bytes. You write one in single or double quotes, and the difference matters. Single quotes are literal: what you type is what you get, except \' and \\. Double quotes understand escape sequences like \n (newline) and \t (tab), and they interpolate variables — "Hi $name" puts the value of $name into the string.
<?php
$name = "Asha";
$qty = 3;
echo 'Hi $name\n', "\n"; // single: no interpolation, no \n
echo "Hi $name, you have $qty items\n";
echo "Tab:\tafter tab | quote: \" | dollar: \$name\n";
// arrays and objects: use braces for anything beyond a plain variable
$user = ['name' => 'Ravi', 'langs' => ['php', 'sql']];
echo "User: {$user['name']}, first language: {$user['langs'][0]}\n";
echo "Simple syntax works for one level: $user[name]\n";
$order = new stdClass();
$order->id = 1042;
$order->total = 59.5;
echo "Order #{$order->id} costs {$order->total}\n";
$item = "apple";
echo "Two {$item}s\n"; // braces mark where the name endsHi $name\n
Hi Asha, you have 3 items
Tab: after tab | quote: " | dollar: $name
User: Ravi, first language: php
Simple syntax works for one level: Ravi
Order #1042 costs 59.5
Two applesRemove the braces in the last line: "Two $items". Read the warning — PHP now looks for a variable called $items.
$user['name']), and double quotes when you interpolate or need \n. The speed difference is a myth — PHP compiles both once. Braces {$...} are the safe default inside double quotes: they work for array keys, nested arrays, properties and method calls like {$order->total()}.Parse error: quoted array key inside a string without braces
<?php
$user = ['name' => 'Asha'];
echo "Hi $user['name']\n";Parse error: syntax error, unexpected string content "", expecting "-" or identifier or variable or number in main.php on line 3Inside double quotes the "simple" syntax allows only $user[name], with the key unquoted. A quoted key confuses the string parser, and the message is famously unhelpful. The rule is easy: anything more than a plain variable goes in braces.
Wrap the expression in braces.
<?php
$user = ['name' => 'Asha'];
echo "Hi {$user['name']}\n";Warning: Array to string conversion
<?php
$tags = ['php', 'web'];
echo "Tags: " . $tags . "\n";Warning: Array to string conversion in main.php on line 3
Tags: ArrayAn array has no automatic string form. PHP warns and prints the literal word Array — so a page can show "Tags: Array" to real users. The same happens with "Tags: $tags".
Decide how the array should look as text: join it with implode(), or debug it with print_r() / var_dump().
<?php
$tags = ['php', 'web'];
echo "Tags: " . implode(", ", $tags) . "\n";