Jump to content

Neater Code?


MySQL_Narb

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/269054-neater-code/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/269054-neater-code/#findComment-1382583
Share on other sites

$_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.

Link to comment
https://forums.phpfreaks.com/topic/269054-neater-code/#findComment-1382635
Share on other sites

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'] : '';

Link to comment
https://forums.phpfreaks.com/topic/269054-neater-code/#findComment-1382697
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.