How does a distance sensor work? Four measuring principles explained

Every distance sensor sends out light or sound and works out the distance from what comes back. Here is how triangulation, time-of-flight, phase shift and ultrasonic echo differ, and when each one makes sense.