In your example you have a six line paragraph, whereas there's only place for at most five lines in the page.
The paragraph is fully typeset, then the lines (as horizontal boxes) are sent to the “recent contributions list” and the “page builder is exercised”, that is, the material in the recent contributions is moved to the main vertical list and TeX does its internal computations to see whether a page break must be issued, and where.
In this case, it obviously has to break pages, so a part of the just typeset paragraph will be on page n, while some other part will be on page n+1. However all \thepage
commands will have already been fully expanded to the value n and nothing can change this any more, because the paragraph has already been typeset.
If instead you use \pageref
, the page number will be assigned from the value gathered from the previous LaTeX run, so they'll be correct unless page breaks have changed in the meantime. Another run of LaTeX will fix the situation.
You can rely upon the value of \thepage
only immediately after a \newpage
command, because such a command ends the paragraph possibily being built and makes TeX exercise the page builder.
Well, one can think to borderline situations where not even at such a point \thepage
is reliable (big floats, for instance).
\label
and\pageref
should be used - due to the asynchronous page builder\thepage
is not reliable in the textbody.\pageref
is the only reliable method.