echo "<input type=\"text\" size=3 name=\"{$item[\"name\"]}\"/>";
but following works fine
echo "<input type=\"text\" size=3 name=\"{$item['name']}\"/>";
As per my understanding \" really escape the "
You don't need to escape the quotes inside the {}. That's supposed to be quoted because it means the string index "name" for the PHP array $name. Without quotes (or escaped quotes), name is treated as a constant (which I'm assuming it isn't), and then you've got an extra pair of quotes which don't belong.
OTOH, this would also be correct:
echo "<input type=\"text\" size=\"3\" name=\"$item[name]\"/>";
(no quotes around name and no {} either)
see http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
re:comment
Any non-keyword without quotes or the $ sign is regarded as a constant (as you can define with the define function), thus "name" (with quotes) is a string, and name without quotes is a constant.
when written like this, however:
echo "{$item[name]}";
PHP will first look for a constant with the name name, then if it doesn't exist, it will use the string "name". However, it will (depending on your settings?) also issue a warning.
Why use " anyway? It turns PHPs special character parsing on, eats more processor time. It is better practice to use '.
This way you don't have to escape all HTML quotation marks so the script will be nicer, shorter and less processor will used for simple text processing, where you just join text with a variable.
echo '<input type="text" size=3 name="'.$item['name'].'"/>"';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With