Do I need a !DOCTYPE declaration in a php file with html?

DanielFox picture DanielFox · Jan 30, 2013 · Viewed 74.4k times · Source

I have a php file with my website content in it. The file needs to be .php because i get some variables first and then use it later in the website content. Like this example:

<?php
$user_name = $_REQUEST['username'];
?>

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>Page Title</title>
    <link rel="stylesheet" href="css/style.css" />
</head>
<body>
Welcome <?php echo $username;?>
</body>
</html>

Do I need the <!DOCTYPE HTML>, since the file extension is php? Also, is it placed corretly? Should it come before the tag or in the very first line of my file?

I also noticed that if I remove the <!DOCTYPE HTML>, some of my css code stops working...

Thank you very much.

Answer

Graham Walters picture Graham Walters · Jan 30, 2013

Yes you need a DOCTYPE as your browser only sees the following part of the above code

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>Page Title</title>
    <link rel="stylesheet" href="css/style.css" />
</head>
<body>
Welcome THE USER NAME
</body>
</html>

I usually place the close PHP tag and the DOCTYPE together like so ?><!DOCTYPE HTML>