Jump to content
StevenOliver

Declaring Variable Hell

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.

Share this post


Link to post
Share on other sites

Set default values
 

$customerNumber = $_POST['customerNumber'] ?? "";
$loop_counter = $_POST['loop_counter'] ?? 0;
// etc

 

Share this post


Link to post
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 :-)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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" 😁

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.