Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

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

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.

question from:https://stackoverflow.com/questions/14613030/do-i-need-a-doctype-declaration-in-a-php-file-with-html

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...