Going a little more off of what muguy said, depending on what’s going on with your iPhone, it may be caused by software, rather than hardware.
Replacing the phone may not fix the issues that you’re having, as if it is a software problem, it’ll most likely follow you around in your backups, so when you restore on the replacement device, you may be having the same issues.
One of the first steps of troubleshooting that Apple advises at their retail stores and AASPs to eliminate software as the cause of the issue. This can be done by doing many things.
First thing to do is head to Settings > Your Name > iCloud > iCloud Backup. There you can backup your iPhone to your iCloud.
Next, in Settings > Your Name > iCloud, make sure that everything has a green toggle. This makes sure that iCloud is syncing all of your data with the cloud (like messages, calendars, and contacts).
Once this is done, allow your iPhone several hours to sync everything. After that, head into Settings > General > Reset > Reset All Settings. This will remove any configurations from the iPhone that may be causing your issues. This should not cause you to lose data.
If the issue still persists, then you may proceed with restoring your iPhone using iTunes or Finder, and setting the device up as new (Do not transfer apps and data). When you sign into your AppleID, things that are syncing (like photos, contacts, and messages) will come back to your iPhone. The only things that won’t are your apps. This is completely eliminates software as the potential candidate for causing the issues.
If you continue to have issues after restoring and setting up as new, then that lets Apple or the AASP know that the device is indeed having a hardware issue, and can be replaced for another unit.
I hope this was able to provide a little more explanation as to why you were advised to erase your iPhone.
Regards,
TurtleGlo