Jump to content

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

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.

Link to comment
https://forums.phpfreaks.com/topic/269054-neater-code/#findComment-1382696
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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