You can have PHP before doctype, PHP will not exist when it really matters. If you get some white space, just reformat the doctype right after closing ?> .
<?php $var = 'foo'; ?><!DOCTYPE html>
As long as you don’t output anything, the browser doesn’t even know that PHP appeared before the start of DOCTYPE, and that the browser doesn’t know will not damage it;)
source share