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 post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...