This is not a problem with PHP. This is basic HTML.
According to the documentation, a <p>
tag may contain Phrasing Content.
Phrasing Content is defined as:
Consists of phrasing elements intermixed with normal character data.
Phrasing Elements are:
a, em, strong, small, mark, abbr, dfn, i, b, s, u, code, var, samp, kbd, sup, sub, q, cite, span, bdo, bdi, br, wbr, ins, del, img, embed, object, iframe, map, area, script, noscript, ruby, video, audio, input, textarea, select, button, label, output, datalist, keygen, progress, command, canvas, time, meter
Notice how <h1>
is not among them.
All this to say that the docs explicitly imply (???) that <p><h1>...</h1></p>
is invalid, and the browser will correct it to its best guess. It should be noted in particular that if you right-click and select View Source (or View Original Source) then you'll see your HTML as you wrote it - it's only in the DOM inspector that you'll see the "corrected" version.