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.
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
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:
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.
Yes, you should have the
DOCTYPE declaration, since what you send the browser is HTML, not PHP.
The declaration tells the browser that you are using HTML 5 and tells it how to render it.
You should always put a Doctype in your html, no matter how the html is built.
The same rules for HTML apply equally, whether they’re plain HTML files, or HTML files with embedded PHP. YOu need a doctype in every circumstance you’d need one in plain HTML.
By the way, there’s whitespace between the end of the first block oh PHP code and the doctype. I seem to remember that whitespace before the doctype can be problematic (though that might only apply to XHTML)
Your final output is HTML which is what uses the doctype. So, yes, you need one. If it is breaking your CSS then your HTML and/or CSS is wrong and needs to be fixed.
PHP really has nothing to do with HTML – it’s just the server-side code spitting out a bunch of text that the browser interprets.
A doctype is a critical part of how the browser interprets the HTML that follows.
When they contain embedded HTML, it’s better to think of
.php files as regular HTML files which the server has been instructed to scan for dynamic (PHP) code.
!DOCTYPE declaration is still necessary for the document to be semantic.
<!DOCTYPE HTML> <table style="position:absolute; bottom:20%; right:40%;" width="150">
if you put
<!DOCTYPE HTML> in php file some styles will not work properly, some alignment problems.