StevenOliver Posted November 17, 2018 Share Posted November 17, 2018 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 17, 2018 Share Posted November 17, 2018 Set default values $customerNumber = $_POST['customerNumber'] ?? ""; $loop_counter = $_POST['loop_counter'] ?? 0; // etc Quote Link to comment Share on other sites More sharing options...
StevenOliver Posted November 17, 2018 Author Share Posted November 17, 2018 (edited) 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 November 17, 2018 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 :-) Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted November 17, 2018 Share Posted November 17, 2018 (edited) 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 November 17, 2018 by mac_gyver Quote Link to comment Share on other sites More sharing options...
StevenOliver Posted November 17, 2018 Author Share Posted November 17, 2018 OMG that is fantastic! Simple, and clean! I wish I could completely relearn PHP as it's written nowadays. The way I "learned PHP" back in 1999 was you leave "Register Globals" set to "ON" and "display errors" set to "OFF" ? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.