Jump to content

Declaring Variable Hell


StevenOliver

Recommended Posts

Am rewriting someone's thousand-line late-90's PHP script. Error-Reporting shows a gazillion 'Notices' -- almost all of them "Undeclared Index" and "Undeclared Variable."

To temporarily correct these Notices, I've simply declared all of them at the top of my PHP page like this:

$_POST["merchandise_name"] = isset($_POST["merchandise_name"]) ? $_POST["merchandise_name"] : '';
$_POST["customerNumber"] = isset($_POST["customerNumber"]) ? $_POST["customerNumber"] : '';
$loop_counter = isset($_POST["loop_counter"]) ? $_POST["loop_counter"] : NULL;
$anotherVariable = isset($_POST["anotherVariable"]) ? $_POST["anotherVariable"] : NULL;
// etc., etc., there's about 50 more below

This looks amateurish and improper. Is there a better way?

Thank you.

Link to comment
Share on other sites

Thank you for answering so quickly! I can already see where assigning default values is better than my blanket list where everything = "".

I will do that now.

When writing code, if a Notice is thrown "undeclared index line 1500," is it better to declare the default value on line 1499? Or at the very top of the page (like in my aforementioned example)?

Thank you.

 

Edited by StevenOliver
To fix spelling error. Most of my PHP errors are spelling errors. If I cannot even spell when asking a question, how can I expect to write PHP :-)
Link to comment
Share on other sites

for $_POST (and $_FILES) data, you should just detect that a post method form was submitted using if($_SERVER['REQUEST_METHOD'] == 'POST') { all the form processing code goes here... } then all form fields, except for unchecked checkboxs/radio-buttons, will be set. you would only need to use isset() or the Null Coalescing Operator for checkbox/radio-buttons. if you have the form processing code for more than one form on a page, you need to add 'control' logic within the request method test that then detects the value in a hidden field, and executes the correct section of form processing code.

next, copying variables to other variables without any reason is just a waste of typing and clutters up the code. if you did nave a good reason, such as if trimming all the data, so that you can detect if all white-space characters were entered, you can do this using one line of code using array_map() with an appropriate call-back function (either php's trim function or a user written recursive trim function.) this will result in the set of trimmed data being placed in an array, where you would access elements in the resulting array variable in the rest of the code, rather than dealing with a bunch of discrete php variables. this will let you operate on the data as a set, using php array functions (see the next point.)

lastly, for form processing code, if you have more than about three form fields, you should dynamically process the form data, by creating an array that defines the expected field names and any processing attributes, such as 'required', what type of validation, then loop over this defining array and use general-purpose code to process the data.

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.