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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

foreach($_POST as $key -> $value) {
should be
foreach($_POST as $key => $value) {
Ray PaseurCommented:
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!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.