The short answer is yes. For primary fermentation many people use a blowoff tube instead of an airlock to avoid this.
One end of the tube attaches to the carboy/bucket/better bottle, and the other end goes into a jug or bucket with water in it. Same concept as an airlock where CO2 can escape but nothing can get in. The blowoff tube will collect the foam instead of it going all over your carboy, or your floor.
There are many ways to do the same thing. If you are dry-hopping, use a 1" tube so the hops are less likely to clog up the tube.