From my experience, this problem is either caused by moisture having gotten inside the button and causing corrosion, or the ribbon cable has started to get cracks - actually, both problems are ultimately the ribbon cable.
If the phone's silver bezel is starting to separate near the top, it will cause stress on the cable and lead to failure. If that is the case, glue it back together before completing the repair.
The ribbon cable is cheap and easy to find online, but it's a little challenging to replace it. The ribbon cable is attached to a lot of other hardware that must all be disassembled. If you are going to replace it, you should make sure that the replacement cable has the logic board connector already installed - not all do. If it's missing, you can desolder the one on the old ribbon cable and swap them, but it takes some skill and the right tools.
If you want to see if there is some repairable moisture damage, you can disassemble the switch itself and try to clean it. It's kind of "taped" together, so you can carefully unwrap it. The switch contact is simply a metal clicker dome on top of two contacts on the ribbon cable. It it had gotten wet in there, it will cause green corrosion that may be cleaned off with alcohol if you're careful. The traces are very small, however, so the corrosion is usually too far to repair. Make sure you also clean the inside of the clicker dome.
I'm making this sound harder than it is, but I want to make it clear that it can be fixed cheaply (less than $20), but the repair may be above many people's skill level.