davej Posted January 3, 2014 Share Posted January 3, 2014 <?php session_start(); ?> then <!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 More sharing options...
Ingolme Posted January 3, 2014 Share Posted January 3, 2014 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 More sharing options...
davej Posted January 3, 2014 Author Share Posted January 3, 2014 (edited) 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> ...or... <?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 January 4, 2014 by davej Link to comment Share on other sites More sharing options...
justsomeguy Posted January 6, 2014 Share Posted January 6, 2014 You can use PHP to output anything you want. If you're outputting an HTML page, then it should be valid HTML. If you're not outputting an HTML page then don't output HTML tags. Link to comment Share on other sites More sharing options...
thescientist Posted January 13, 2014 Share Posted January 13, 2014 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 More sharing options...
davej Posted January 13, 2014 Author Share Posted January 13, 2014 When do you need to send a header or a status code? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 13, 2014 Share Posted January 13, 2014 Every HTTP response has headers and a status code. The default status code that PHP will send is 200, which means OK.http://en.wikipedia.org/wiki/List_of_HTTP_status_codes Link to comment Share on other sites More sharing options...
thescientist Posted January 13, 2014 Share Posted January 13, 2014 (edited) &nbsp; When do you need to send a header or a status code?&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 January 13, 2014 by thescientist Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now