email form is throwing out fatal error

There is no  syntax error  in the code, but the email form when i tested it is giving this statement,

Fatal error: Cannot access empty property in /home/content/64/11583764/html/summer/test2/contact.php on line 13

	$to = '';
	$subject = 'Feedback from summer rain wellness page';
	//list expected fields
	$expected = array('name','email','comments');
	//setrequired fields
	$required = array('name','comments');
	//create empty array for missing fields
	$missing = array();
	//process the $_POST variables
	foreach($_POST as $key -> $value) {
		//assign to temporary variable bd strip whitespace if not an array
		$temp = is_array($value) ? $value : trim($value);
		// if empty nd required add to $missing array
		if (empty($temp) && in_array($key,$required)){
		//otherwise, assign to a variable of the same name as $key
		elseif (in_array($key,$expected)){
			${key}= $temp;

	//go headonly if all requiredfileds OK
	//build the message
	$message = "Name: $name\n\n";
	$message .= "Email: $email\n\n";
	$message .= "Comments: $comments";
	//limit line length to 70 characters
	$message = wordwrap($message,70);
	//sent it
	$mailSent = mail($to, $subject, $message);
	if ($mailSent){
		//$missing is no longer needed if the email is sent so unset it 

Open in new window

Who is Participating?
Ray PaseurConnect With a Mentor Commented:
It's a common typographical error.

The -> syntax is used with objects.  $_POST is an array.  These are different data types.  But that aside, iterating over $_POST is an anti-practice because it can make your script dependent on unfiltered external data.  If a professional programmer wrote that he would get fired in his first code review.

If you're new to PHP you might find some value in this article that can point you to some good learning resources, and more importantly steer you away from bad learning resources!
GaryConnect With a Mentor Commented:
foreach($_POST as $key -> $value) {
should be
foreach($_POST as $key => $value) {
imagekrazyAuthor Commented:
Thank you both, and Ray I am reading that article the one you suggested, but quick question-- so what do you think of the author(David Powers) who wrote PHP Solutions "Dynamic web design made easy"
Ray PaseurCommented:
I've never read Powers' book, but it appears to be well-reviewed.  Like most things in the technical bookstore, getting the latest copy is very important.  There are things we would have done in 2009 that are now understood to be a bad idea.

Anyway, thanks for the points, and if you're using a book to guide your learning you will be w-a-a-a-y ahead of anyone who is using trial and error!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.