Global Variable Not working

NOTE: I deleted this post just moment after posting when I finally spotted the errors so not sure how you replied.

What would cause a $_POST to give such an error? I thought it was a global that was built into PHP.

PHP Parse error: syntax error, unexpected variable “$_POST”

on this lines:

if (isset($_POST['Prefix']) && isset($_POST['PhoneDay']) $_POST['PhoneDay'] = $_POST['Prefix'] .-. $_POST['PhoneDay'];
if (isset($_POST['Prefix']) && isset($_POST['PhoneMobile']) $_POST['PhoneMobile'] = $_POST['Prefix'] .-. $_POST['PhoneMobile'];
if (isset($_POST['Prefix')) unset($_POST['Prefix']);

The error is a syntax error, meaning that the syntax isn’t correct. It doesn’t mean that the variable is not working.

You are missing the closing ) in the if () {} statements (3 places). You are also missing a ] in the 3rd line of code. You are also missing quotes around the - in the concatenation statements.

I recommend that you always use {} around the statements that are part of a conditional statement/loop.

Also, except for unchecked checkbox/radio fields, all other form fields will be set once the form has been submitted. After you have detected that a post method form has been submitted - if ($_SERVER['REQUEST_METHOD'] === 'POST') {, there’s no need to test if these always set fields are set, because they will be, unless you have a mismatch between the form and your php code, in which case you do want to see any errors, not hide them by using isset(). Perhaps you wanted to test if $_POST[‘Prefix’] is not an empty string !=='' before using it?

You should also trim all input data, mainly so that you can detect if all white-space characters were entered, before validating and using it.

Also, there’s no good reason to unset $_POST data in your code.

I deleted this question almost the instant it was posted so not sure how you replied as I couldn’t even do so without reinstating it.

I do have a reason for unsetting a $_POST value or I wouldn’t have done so but it is unrelated to this question.

The edit history is available to everyone, even a non logged in user, by clicking on the orange pencil icon on the top right of a post.

Sponsor our Newsletter | Privacy Policy | Terms of Service