MySQL_Narb Posted October 3, 2012 Share Posted October 3, 2012 I have a field in my database called long_name (tinytext), what would be considered the neater code? (the below codes represent the value of what it will be when entered into the database) isset($_POST['long_name']) ? $_POST['long_name'] : '' or $_POST['long_name'] Is it really necessary, if the field from a form is empty, you should replace the null with '' Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/269054-neater-code/ Share on other sites More sharing options...
requinix Posted October 4, 2012 Share Posted October 4, 2012 If you're simply talking about whether the field has a value and not about whether the form was submitted, the isset() stuff is partially redundant but mostly useless: $_POST['long_name'] will be an empty string already and thus isset() will always return true. Quote Link to comment https://forums.phpfreaks.com/topic/269054-neater-code/#findComment-1382583 Share on other sites More sharing options...
cyberRobot Posted October 4, 2012 Share Posted October 4, 2012 $_POST['long_name'] will be an empty string already and thus isset() will always return true. That depends. If "long_name" comes from a checkbox or radio button, for example, the POST variable may not be set. If the page is ever called without the form submission, the POST variables won't be set. Quote Link to comment https://forums.phpfreaks.com/topic/269054-neater-code/#findComment-1382635 Share on other sites More sharing options...
requinix Posted October 4, 2012 Share Posted October 4, 2012 That depends. If "long_name" comes from a checkbox or radio button, for example, the POST variable may not be set. I was going with the assumption that since the field is a TINYTEXT that it would contain text. Thus a textbox or textarea. Quote Link to comment https://forums.phpfreaks.com/topic/269054-neater-code/#findComment-1382696 Share on other sites More sharing options...
Psycho Posted October 4, 2012 Share Posted October 4, 2012 Personally, I don't think the DB type has any bearing on the question. It all depends on "how" the POST value is to be used. Personally, I use an isset check before referencing ANY POST values. I may have the same backend code that works with different inputs. So, if the value is not included in the POST data I will set to false or an empty string - again based upon how I will use the value in the code later. $long_name = isset($_POST['long_name']) ? $_POST['long_name'] : ''; Quote Link to comment https://forums.phpfreaks.com/topic/269054-neater-code/#findComment-1382697 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.