2
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 "

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

can you please elaborate more in this. i am little confused here $item is an array and need to be index by string "name" as opposed to name which is const.
Thanks Mark. You made it very clear.So for robustness of the code I should not even use echo "{$item[name]}"; because if the name constant exist (somewhere else in the code) the result of evaluation will be different from when it (name constant) does not exist.
it means echo "<input type=\"text\" size=3 name=\"".$item["name"]."\"/>"; is more robust in all cases.
@David: No! If you use quotes around the key, you have to use {} also. If you don't use {} then don't put quotes.
2

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'].'"/>"';

Comments

2

$item["name"] is the complete name for the variable so you can not build the variable name when you are using some of the print functions.

In this case it is better to concatenate the strings instead of inserting the variable in an implicit way. e.g.

echo "<input  type=\"text\" size=3 name=\"" . $item["name"] . "\"/>"; 

or something like

printf("<input  type=\"text\" size=3 name=\"%s\"/>", $item["name"]); 
//Parameters info at http://www.php.net/manual/es/function.sprintf.php

Comments

1

It's one of those edge cases that you've just got to deal with because of the interpreter. Nothing special.

In $item[\"name\"], the backslash \ is illegal and shouldn't be there, so you get your syntax error.

Comments

-1

Think what about what it would like after escaping

echo "<input  type=\"text\" size=3 name=\"{$item[\"name\"]}\"/>";

would become

<input  type="text" size=3 name="{$item["name"]}"/>";

which you can see is wrong as the quotes after the name attribute would terminate at the second quote leaving the rest hanging

1 Comment

as far as my understanding $item["name"] is replaced by the php interpreter with a value let say it is xyz so the final output will <input type="text" size=3 name="xyz"/>";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.