Jump to content

<!DOCTYPE html> ?


Recommended Posts

<?php session_start(); ?>


<!DOCTYPE html><html><body>

before first echo?


There are tons of Php examples where no doctype or html tags are used, and they aren't all Ajax examples.


Link to comment
Share on other sites

The <!DOCTYPE> tag itself is the same as something being printed with echo. There's no difference between <!DOCTYPE html> and <?php echo '<!DOCTYPE html>'; ?>

The only things that have to be put before printed content is sent are functions that send headers, such as setcookie(), header() and session_start().


In many systems you don't see HTML anywhere because it's being loaded from a template file. PHP can be used to output more than just HTML, it can output image data, plain text or a PDF document among other things.

Link to comment
Share on other sites

So if I have a Php file with an echo that is not submitting a response to Ajax then that Php file should "package up" the echo so that it appears in the body of an html file, right? Or not?

<?php session_start(); ?><!DOCTYPE html><html><body><?php echo "various {$data}";[...]?></body></html>


<?php session_start();echo "<!DOCTYPE html><html><body>";echo "various {$data}";[...]echo "</body></html>";?>

..but not just spewing data without the "html packaging up" such as like this...

<?php session_start();echo "various {$data}";[...]?>

...unless it is for generating an Ajax response, which is what I'm guessing is the purpose of the text, pdf or image data you mentioned?

Edited by davej
Link to comment
Share on other sites

For example, I use PHP with the Slim framework as a backend API to my websites. All I do is output JSON to the front end consumers of these webservices. No HTML at all. Just headers, status, and response body.

Link to comment
Share on other sites


When do you need to send a header or a status code?

&amp;nbsp;For every API route I create. http://www.w3.org/Protocols/HTTP/HTRESP.htmlI should also clarify that I develop RESTful APIs for my projects.http://www.infoq.com/articles/rest-introductionSo far I have gotten away with just using Content-Type: application/json for the headers, but the status is dependent on the request, and also how the response gets processed.Every request could have a different response based on whether certain conditions where meet (Not Logged in - 401), or if the user forgot a parameter (Bad Request - 400), or they make the request to an unavailable resource (Not Found - 404), or if a resource was created successfully (Created - 201), etc. The idea is to make the webservice useful and informative to the consumer. In that way, you use status codes and additional headers and the response body to communicate the success, or failure, of said request. Edited by thescientist
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...