Diagram of the string "…abcdefn": the dollar anchor matches both before the final newline and at the end of the string, z matches only at the end; verdict tables show PHP preg_match and Python re accepting the lenient pattern and rejecting the strict ones.
PHP

A ^…$ regex accepts one trailing newline in PHP and Python — the strict anchors are /D, \z and re.fullmatch

Without /m, a ^…$ regex accepts a value ending in a newline, in PHP preg_match and Python re alike. Strict forms: the D modifier, \z, re.fullmatch.