Phase retrieval and phase diversity are wavefront sensing techniques fed by focal-plane data. In phase retrieval, the incoming wavefront is estimated from a single (near-) focal image of an unresolved source. In phase diversity, from at least two images of the same (complex) object recorded in presence of a known optical aberration (e,g., defocus), both the unknown incoming wavefront and the observed object can be derived. These two techniques have many advantages: the hardware is limited to (or can be merged in) the usual imaging sensor, the number of estimated modes can be continuously tuned and both are among the very few methods enabling the measurement of differential pistons/tip/tilts on segmented or divided apertures. The counterpart is that complexity is reported to digital processing, which is either iterative and long, or fast but limited to a first-order phase expansion. Based on an innovative physical approach and mathematical inversion, new simple, analytical and exact algorithms have been recently derived for phase retrieval and diversity. Conjugated with recent detector and processor advances, these algorithms can be implemented in adaptive/active optics loops, or even provide a purely-digital on-the-fly alternative. In this paper, for the first time, we present experimental validation of these algorithms with the cophasing of a segmented mirror.