it come to my email id but i am getting this error...Syntax error '$names' 9T_VARIABLE) plz help me

here is my code

<? php

$names = $_POST['name'];
$visitor_email = $_POST['email'];
$visitor_phone_no = $_POST['phone'];
$message = $_POST['Message'];

$email_from = "rkenterprises9083@gmail.com";
$email_subject ="New Form Submission";
$email_body ="User Name: $names.\n".
"User Email: $visitor_email.\n".
"User Phnoe: $visitor_phone_no.\n".
"User message: $message.\n";

$to = "nishantverma040@gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply_To: $visitor-email \r\n";

mail($to,$email_subject,,$email_body,$headers);
header("Location: index.html");
?>

Dani AI

Generated

Quick summary: the "unexpected '$names' (T_VARIABLE)" parse error means PHP never saw a valid open tag or there’s a stray syntax token before that variable. already flagged the immediate syntax problems, which will stop the parser from reaching your variable declarations. Fixing the opening tag and the small syntax issues will remove the parse error — but there are a few additional checks and hardening steps that will make the form handling reliable and safe.

Checklist to run through (in order):

  • Ensure the PHP opening tag is exactly <?php with no spaces; otherwise the parser won’t start. pointed this out.
  • Avoid hyphens in variable names and use consistent $_POST keys (case matters in your form names). Match the form field names to your PHP keys.
  • Verify the mail() call has the correct number and order of arguments (no accidental extra commas) and that header names use the correct format (for example the standard header keys).
  • Sanitize and validate inputs before using them (especially email). Strip CR/LF from any values used in mail headers to prevent header injection.
  • Check the return value of mail() and your server’s mail logs if messages aren’t delivered; many hosts restrict mail() or require SMTP.

Tiny, useful snippets to help debug (add at top while developing):

ini_set('display_errors', 1);
error_reporting(E_ALL);

Validate email before sending:

if (!filter_var($visitor_email, FILTER_VALIDATE_EMAIL)) {
  // handle invalid email
}

Additional tips: remove any user-supplied CR/LF from header fields (e.g., str_replace(array("\r","\n"), '', $value)), call exit; after a Location redirect to stop further output, and consider using a maintained library like PHPMailer for robust SMTP, authentication, and clearer error messages. Once those fixes and checks are in place, re-run the script and the parse error should be gone; if a new error appears, paste the exact message and the minimal code around that line for targeted help.

Recommended Answers

All 3 Replies

Hi,

What is the complete error message? PHP error messages typically say what line the error is on, etc.

In investigating the code above, the biggest things that stand out ot me are on line 17 you have the variable $visitor-email but I don't believe that dashes are allowed in variable names. Secondly, on line 19, you have two commas after $email_subject.

commented: It's Dani Lint. Is there a Lint checker for PHP? (Yes!) +15

this is the error......Parse error: syntax error, unexpected '$names' (T_VARIABLE) on line 4

Change the first line from <? php to <?php

You have a space between the ? and the php that needs to be removed. Also, make the other changes I wrote about earlier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.