Jump to content
Larry Ullman's Book Forums

Why Do Single Quotes In Array Keys Cause Parse Errors?


Recommended Posts

I'm asking this question based on the section on p.57, "Receiving Form Data in PHP".

 

When a form uses method="post" why does

 

print "($_POST['name'])";

 

cause an error, whilst

 

print "fe'rg'al";

 

doesn't? Both statements have single quotes inside double quotes.

 

Thanks!

Link to comment
Share on other sites

It isn't the single quotes that causes the problem. The bottom is just a string, as long as you match the quotes properly it's all good. The top line is an associative array and can't be called as such.

print "{$_POST['name']}"

should work. print() doesn't need to take the parenthesis and to call an associative array inside a print or echo you need to wrap it in curly brackets. As far as I know

Link to comment
Share on other sites

Thanks for the reply Jonathon, I'm not familiar with curly brackets yet, but will hopefully get to them soon.

 

As regards to using print with arrays, like the one I referred to above, Larry wrote the following in the book;

 

When used within double quotation marks, the single quotation marks around the key will cause parse errors
Link to comment
Share on other sites

You don't actually need the curly brackets in the second example (when doing concatenation) and you don't need parentheses in the first.

 

As for why this is a problem, I don't know: just the way PHP works, I guess.

Link to comment
Share on other sites

 Share

×
×
  • Create New...