If the refractive index varies along the path, the OPL is given by
An electromagnetic wave that travels a path of given optical path length arrives with the same phase shift as if it had traveled a path of that physical length in a vacuum. Thus, if a wave is traveling through several different media, then the optical path length of each medium can be added to find the total optical path length. The optical path difference between the paths taken by two identical waves can then be used to find the phase change. Finally, using the phase change, the interference between the two waves can be calculated.
Fermat's principle states that the path light takes between two points is the path that has the minimum optical path length.