Fergal 0 Posted August 26, 2011 Report Share Posted August 26, 2011 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! Quote Link to post Share on other sites
Jonathon 255 Posted August 26, 2011 Report Share Posted August 26, 2011 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 Quote Link to post Share on other sites
Fergal 0 Posted August 27, 2011 Author Report Share Posted August 27, 2011 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 Quote Link to post Share on other sites
Jonathon 255 Posted August 27, 2011 Report Share Posted August 27, 2011 Well in that case it is the single quotes I just assumed it was because you're accessing an array rather than a string. Quote Link to post Share on other sites
Antonio Conte 426 Posted August 29, 2011 Report Share Posted August 29, 2011 Correct: print $_POST['name']; Correct: print 'I have some text before '.$_POST['name'].' and some text after'; Correct: print "I have some text before {$_POST['name']} and some text after": Edit: Removed ( & ). Quote Link to post Share on other sites
Larry 433 Posted August 29, 2011 Report Share Posted August 29, 2011 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. Quote Link to post Share on other sites
Fergal 0 Posted August 31, 2011 Author Report Share Posted August 31, 2011 Thanks for the replies everyone! Quote Link to post Share on other sites
Recommended Posts
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.