garyed Posted December 15, 2021 Share Posted December 15, 2021 I have a lot form variables where I'm having the errors show up since I updated my php version on my webhost server. Since I have well over 100 variables, it's going to take a lot of time to correct them so i just disabled errors for now in the php.ini file to keep things working. I was wondering that since form variables are in kept an array, if there isn't an easy way to have them all initiated when the page loads. I think that would solve the majority of the errors & there wouldn't be that many left. Any ideas appreciated? Quote Link to comment https://forums.phpfreaks.com/topic/314316-best-way-to-stop-undefined-variable-errors/ Share on other sites More sharing options...
garyed Posted December 15, 2021 Author Share Posted December 15, 2021 On pages where there are only a few variables I've tried : if (!isset($_POST['var1'])) { $var1=""; } else { $var1=$_POST['var1']; } That works but I don't want to have to do that with every variable. Quote Link to comment https://forums.phpfreaks.com/topic/314316-best-way-to-stop-undefined-variable-errors/#findComment-1592704 Share on other sites More sharing options...
mac_gyver Posted December 15, 2021 Share Posted December 15, 2021 (edited) unconditionally doing this will end up hiding programming mistakes and when hackers/bot-scripts submit form data that doesn't include expected fields. you would want to see (when developing) or log (when on a live server) the errors so that you know what is occurring and can fix problems. where exactly are the errors occurring at? if it is when you are populating form field values, before the form has ever been submitted, the ideal solution would be to use php's null coalescing operator at the point of using the value. however, your example above indicates you are copying post variables to discrete variables (inside the form processing code?). you should instead keep the form data as a set, in a working array variable, usually having been trimmed, then operate on elements of this array variable throughout the rest of the code. doing this supports dynamically validating and processing the data. Edited December 15, 2021 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/314316-best-way-to-stop-undefined-variable-errors/#findComment-1592705 Share on other sites More sharing options...
gizmola Posted December 15, 2021 Share Posted December 15, 2021 You can use the empty function now to kill 2 birds with one stone. if (!empty($_POST['var1']) { // Make some use of $_POST['var1'] } There is also the "null coalescing" syntax (php >= 7) you can use for assignments like this: $user = $_POST['user'] ?? 'Guest'; // This is equivalent to $user = isset($_POST['user']) ? $_POST['user'] : 'Guest'; Quote Link to comment https://forums.phpfreaks.com/topic/314316-best-way-to-stop-undefined-variable-errors/#findComment-1592707 Share on other sites More sharing options...
benanamen Posted December 15, 2021 Share Posted December 15, 2021 52 minutes ago, gizmola said: You can use the empty function now to kill 2 birds with one stone. You would first need to trim the POST array as a space would bypass the check. Quote Link to comment https://forums.phpfreaks.com/topic/314316-best-way-to-stop-undefined-variable-errors/#findComment-1592708 Share on other sites More sharing options...
garyed Posted December 15, 2021 Author Share Posted December 15, 2021 I really appreciate the ideas but I have to admit that I don't understand the examples. I have no idea what the : ?? 'Guest'; does or even means. I am actually learning as I'm going, sort of on an as needed basis. Things that may seem obvious to the experienced programmer, are not obvious to me. I'm going to do a little testing with some of this stuff & see if i can make it work. Quote Link to comment https://forums.phpfreaks.com/topic/314316-best-way-to-stop-undefined-variable-errors/#findComment-1592709 Share on other sites More sharing options...
kicken Posted December 15, 2021 Share Posted December 15, 2021 4 hours ago, garyed said: I have no idea what the [??] does or even means The Null Coalescing operator provides a fallback value if the previous value is null (or doesn't exist). $displayName = $_POST['displayName'] ?? 'Anonymous'; Using ?? there is a shorter way of writing if (isset($_POST['displayName'])){ $displayName=$_POST['displayName']; } else { $displayName='Anonymous'; } Using ?? to provide a fallback value prevents PHP from complaining about undefined variables/indexes. If you don't have an appropriate default value to provide, you can just use null. Quote Link to comment https://forums.phpfreaks.com/topic/314316-best-way-to-stop-undefined-variable-errors/#findComment-1592712 Share on other sites More sharing options...
ginerjm Posted December 15, 2021 Share Posted December 15, 2021 My suggestion on this new NULL operator would be to use the OR ( || ) operator rather than ?? so to the reader it says "value or 'default' " Quote Link to comment https://forums.phpfreaks.com/topic/314316-best-way-to-stop-undefined-variable-errors/#findComment-1592720 Share on other sites More sharing options...
garyed Posted December 15, 2021 Author Share Posted December 15, 2021 6 hours ago, kicken said: The Null Coalescing operator provides a fallback value if the previous value is null (or doesn't exist). $displayName = $_POST['displayName'] ?? 'Anonymous'; Using ?? there is a shorter way of writing if (isset($_POST['displayName'])){ $displayName=$_POST['displayName']; } else { $displayName='Anonymous'; } Using ?? to provide a fallback value prevents PHP from complaining about undefined variables/indexes. If you don't have an appropriate default value to provide, you can just use null. "Now I see", says the blind man Thanks for the explanation. Now it looks like the only problem I'll have to be careful about is not having this type of universal fix to affect my sessions too. Quote Link to comment https://forums.phpfreaks.com/topic/314316-best-way-to-stop-undefined-variable-errors/#findComment-1592721 Share on other sites More sharing options...
garyed Posted December 16, 2021 Author Share Posted December 16, 2021 I've tried these two options & they seem to work OK. The null option works better if I'm using the variable for any mathematical calculations. Is there anything wrong with declaring all variables that might be used in a form with the null fallback value as I did below? $var=$_POST['var']; ?? ''; $var=$_POST['var']; ?? null; Quote Link to comment https://forums.phpfreaks.com/topic/314316-best-way-to-stop-undefined-variable-errors/#findComment-1592780 Share on other sites More sharing options...
gizmola Posted December 17, 2021 Share Posted December 17, 2021 As for whether you should set POST variables to null vs '', an argument could be made that they have similar strengths and weaknesses. I would suggest always setting empty form variables initially to '' rather than null for 2 reasons. From the browser point of view everything coming from a POST method is in string format. It is up to you to synchronize your form code with the datatypes you received. That's a different topic, but suffice it to say that loose typing and ease of conversion between types is one of the features of working with PHP, so long as you are consistent. This issue exists when using null and comparing to a variable that doesn't even exist. <?php $foo = ''; $bar = null; echo $foo === $baz ? "=" : "!="; echo PHP_EOL; echo $bar === $baz ? "=" : "!="; //output // != // = In PHP 8, they changed things, so this now produces a warning, but in prior versions, trying to use a variable that was never declared just produces a notice. This is where it gets muddy with PHP, in that a variable set = null is equivalent to a variable that doesn't even exist. In general null exists to support the concept of "no value". There is no intrinsic value to setting a variable you intend to use as a number = null vs. '' other than that with '' it's internally typed initially as a string. Perhaps a more important comment to you is that, having lots of variables floating around isn't great. Typically it is better if you have some sort of object or at least an array that supports your form handling. It's also pretty far from DRY when you are writing the same statement 10x because you are processing a form with 10 elements. I would start down this path, using an array, with a name like $postData or $formData. Look at the benefit of something like this: function getPostData($fields) { $postData = []; foreach ($fields as $field) { $postData[$field] = $_POST[$field] ?? ''; } return $postData; } $fields = ['name', 'description', 'email_address']; $postData = getPostData($fields); It's a lot easier to build upon or expand this concept, then to write code with a bunch of independently named variables. Using an array will provide you many benefits. Classes can do even more for you, and if you look at frameworks you will find they always wrap the underlying HTTP request/response mechanics for you using classes. Quote Link to comment https://forums.phpfreaks.com/topic/314316-best-way-to-stop-undefined-variable-errors/#findComment-1592787 Share on other sites More sharing options...
garyed Posted December 17, 2021 Author Share Posted December 17, 2021 That seems like a great idea. I wish I had done that before building my site. I've can have as much as 1,000 variables on one page so it's going to be a lot of work no matter what I do now. Thanks again for the ideas Quote Link to comment https://forums.phpfreaks.com/topic/314316-best-way-to-stop-undefined-variable-errors/#findComment-1592789 Share on other sites More sharing options...
ginerjm Posted December 17, 2021 Share Posted December 17, 2021 Do you really want to present a page with 1000 inputs on a page for your users to have to work with? Quote Link to comment https://forums.phpfreaks.com/topic/314316-best-way-to-stop-undefined-variable-errors/#findComment-1592793 Share on other sites More sharing options...
mac_gyver Posted December 17, 2021 Share Posted December 17, 2021 39 minutes ago, garyed said: I've can have as much as 1,000 variables on one page so it's going to be a lot of work no matter what I do now. when you have more than about 2-3 form fields, you should use a data-driven design and dynamically validate and process the form data. based on the copying of post variables to discrete variables, apparently this information was not seen - On 12/14/2021 at 5:56 PM, mac_gyver said: however, your example above indicates you are copying post variables to discrete variables (inside the form processing code?). you should instead keep the form data as a set, in a working array variable, usually having been trimmed, then operate on elements of this array variable throughout the rest of the code. doing this supports dynamically validating and processing the data. at the point of populating form field values, again, if you have more than just a few of them, you need to switch to a templating system where you would supply an array of values as the input to the template, rather than use a discrete variable for each one. Quote Link to comment https://forums.phpfreaks.com/topic/314316-best-way-to-stop-undefined-variable-errors/#findComment-1592794 Share on other sites More sharing options...
garyed Posted December 17, 2021 Author Share Posted December 17, 2021 23 minutes ago, ginerjm said: Do you really want to present a page with 1000 inputs on a page for your users to have to work with? It's a calculation program that defaults to about 50 inputs but if the user chooses, it can go up to 1,000 inputs. Quote Link to comment https://forums.phpfreaks.com/topic/314316-best-way-to-stop-undefined-variable-errors/#findComment-1592796 Share on other sites More sharing options...
mac_gyver Posted December 17, 2021 Share Posted December 17, 2021 39 minutes ago, garyed said: It's a calculation program that defaults to about 50 inputs this is a set of data where you will be operating on each element in the set in the same/similar way. arrays are for sets of data. you should be using arrays for the form fields and loop over the submitted arrays of data. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314316-best-way-to-stop-undefined-variable-errors/#findComment-1592797 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.