Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

php

doctype

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.

like image 266
DanielFox Avatar asked Jan 30 '13 20:01

DanielFox


People also ask

What is DOCTYPE HTML in PHP?

Definition and Usage All HTML documents must start with a <! DOCTYPE> declaration. The declaration is not an HTML tag. It is an "information" to the browser about what document type to expect.

Can we include PHP file in HTML file?

As you can see, you can use any HTML you want without doing anything special or extra in your PHP file, as long as it's outside and separate from the PHP tags. In other words, if you want to insert PHP code into an HTML file, just write the PHP anywhere you want (so long as they're inside the PHP tags).

Is DOCTYPE mandatory in HTML?

The HTML document type declaration, also known as DOCTYPE , is the first line of code required in every HTML or XHTML document. The DOCTYPE declaration is an instruction to the web browser about what version of HTML the page is written in.

Can we write HTML without DOCTYPE?

Without a Doctype: The purpose of DOCTYPE is to tell the browser what type of HTML you are writing. It is not valid to omit the DOCTYPE. There is no “Standard” format. The browser will just try to parse HTML as best it can.


2 Answers

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>

like image 73
Graham Walters Avatar answered Sep 30 '22 16:09

Graham Walters


Sorry to resurrect the dead, but no one seems to explain why you need a doctype in HTML (yes a PHP script outputting HTML is an HTML file at the end).

Declaring a doctype affects the way the browser interprets your HTML (this is probably why your css code may stop working without a doctype). Basically there's 2 ways: quirks mode and strict mode. The latter is sticking to standards, so you should always tell the browser which HTML standard your code is following (nowadays you probably want HTML5 which has the simplest doctype: <!DOCTYPE html>).

See here for a more detailed explanation.

like image 23
mTorres Avatar answered Sep 30 '22 17:09

mTorres