Click here to Skip to main content
15,881,797 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Please help me fix this error message I have received. It says the error is with line 9. Here's the code.

<?php

$contacts = array("entered_email_adress@yourdomain.com", "Zoho_CRM@yourdomain.com", "Mail_Chimp@yourdomain.com");

foreach($contacts as $contact) 
{
	$to = $contact;

	else if($contact == "entered_email_adress@yourdomain.com")
	{
		$subject = 'Web Form Inquiry';
		$subject = 'the subject';
	}
	else if($contact == "Zoho_CRM@yourdomain.com")
	{
		$subject = 'the subject';
		$message = "First Name" + ", " + "Last Name", + " + " "Company", + ", " + "Email"
	}
	else if($contact == "Mail_Chimp@yourdomain.com")
	{
		$subject = 'the subject';
		$message = "First Name" + ", " + "Last Name"
	}

	mail($to, $subject, $message, $headers);
}

?>


What I have tried:

I've been searching for a solution ever since I received the error message.
Posted
Updated 13-May-22 3:41am

You can try to replace each else if with elseif.
This statement has no space between words in PHP.
 
Share this answer
 
Comments
[no name] 24-May-19 12:54pm    
Also a 5, looks like php needs elseif
phil.o 24-May-19 12:58pm    
Yep; I missed the first else, though.
You start with an else if
else if($contact == "entered_email_adress@yourdomain.com")

I think it should be more only an if
if($contact == "entered_email_adress@yourdomain.com")

Note: I'm not experienced in php...

[Edit]
Think also about if no condition matches. In this case you should not call mail?
 
Share this answer
 
v3
You have 4 'else if' statements but there is no initial 'if' statement.
Your code should look something like ...

<?php

$contacts = array("entered_email_adress@yourdomain.com", "Zoho_CRM@yourdomain.com", "Mail_Chimp@yourdomain.com");

foreach($contacts as $contact) 
{
	$to = $contact;

	if (some test)
	{
		do_something();
	}
	else if($contact == "entered_email_adress@yourdomain.com")
	{
		... as in your original code

or, probably what you intended ...
<pre><?php

$contacts = array("entered_email_adress@yourdomain.com", "Zoho_CRM@yourdomain.com", "Mail_Chimp@yourdomain.com");

foreach($contacts as $contact) 
{
	$to = $contact;

	if($contact == "entered_email_adress@yourdomain.com")    // With just 'if', not with 'else if'
	{
		... as in your original code
 
Share this answer
 
Comments
[no name] 24-May-19 12:24pm    
Explained better than me, so a 5
Member 14463408 24-May-19 12:45pm    
Thank you for your solution, and yes, removing else did seem to fix the error on Line 9, but now I have a new (Syntax error, unexpected ',') message relating to line 17. Anything else I'm forgetting to fix?

[no name] 24-May-19 12:57pm    
See the solution of phil.o. use elseif instead of else if

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900