As others have said, !DOCTYPE
is necessary in php scripts that are outputting HTML. If you were creating an image or executing a bash file or something, the story would be different.
As for where it belongs, it's a good idea to put it at the start just so you don't accidentally output something before it, but if you are using session variables or sending headers, you will want to make sure to do those things BEFORE declaring a doctype. Remember, there can be NO browser output (even whitespace) before headers are sent through php or sessions are started.
https://stackoverflow.com/questions/14613030/do-i-need-a-doctype-declaration-in-a-php-file-with-html