0

Is there a way to get PHP to render HTML semantically? Whenever I would preview my script in 'view source' the HTML rendering is like compacted together in one line and hard to read. Just wondering if there is such a way or maybe just how I type my PHP

Jose Gomez
  • 43
  • 10
  • 3
    add line breaks? pass through html tidy, copy and paste to html editor and use its formatting function ... –  Apr 25 '12 at 21:16
  • Try formatting strings with \n. – Leri Apr 25 '12 at 21:16
  • Why? It's advantageous to not have all of those line breaks in there, as it saves a few bytes along the way. Use your browser's inspect tools to see the HTML as interpreted in a tree. I've never had a need for this. – Brad Apr 25 '12 at 21:17
  • 1
    @Brad do you really care about saving the few bytes and making the source hard for you to use? –  Apr 25 '12 at 21:18
  • @Dagon, I find a whole bunch of extra `\n` in my PHP far more harder to use than simply inspecting the source of the page in my browser, which formats it for me. Why would you ever write something to format your HTML for you, when you're writing PHP? It makes no sense. – Brad Apr 25 '12 at 21:19
  • because it makes debugging easier, which is what the op is asking about. –  Apr 25 '12 at 21:21
  • Could you post up an example of your source php file and the HTML source output? Maybe use something like http://codepad.org/ or http://jsfiddle.net and add a link here if it's a lot of code to copy and paste into an edit of your original post. – Squig Apr 25 '12 at 21:24
  • @Dagon, What exactly do you find difficult about this: http://lifehacker.com/assets/images/lifehacker/2008/09/inspector-for-web-devs.png You don't need any extra crap in your code, you don't need to waste CPU cycles reformatting your output, **and** you still get to see your HTML in a nice way. I don't follow why you think this is problematic, and why you insist on doing things the hard way. – Brad Apr 25 '12 at 21:29
  • 1
    @Brad because sometimes what your are trying to debug is malformed HTML. And the inspector makes assumptions about the DOM that are incorrect and the tree doesn't accurately reflect the HTML. Or because most inspectors show the DOM, not the tree form of the HTML, so if some JS edits the DOM the inspector shows that rather than the output HTML. – ben Apr 25 '12 at 21:59
  • @ben, Yes, I am aware of that, but haven't had a problem in the last decade that warranted adding needless line breaks and tabs to the output. Whatever, to each their own. If you want to add needless crap to your code, go ahead. Hopefully you're only working on your own personal website, and aren't contributing code to a project that the rest of us have to deal with at some point. – Brad Apr 25 '12 at 23:10
  • wow I didn't think it would turn into a debate. but what I'm asking is when my scripts turn into 10 blocks of lines compressed into 1 line. and by semantics I meant I want the php to render with proper indentation and spacing like most said above – Jose Gomez Apr 25 '12 at 23:16
  • turns this http://jsfiddle.net/unhmf/ into http://jsfiddle.net/hhq8b/ what @Dagon said it will make my debugging much easier. – Jose Gomez Apr 25 '12 at 23:19
  • @Brad I get your point, as well as everybody's. I personally do not like to view others source code thats ugly and hard to read. I've learned to read code with proper indention and proper spacing that when my PHP is rendered it's like wow, I don't want others to see this and think I'm a terrible coder as in writing code semantically. – Jose Gomez Apr 25 '12 at 23:22
  • @yeahthatguyrightthere, If I were to see compacted HTML output from your PHP script, I'd **never** say that you were a terrible coder. Only from seeing the source code could someone suggest that your formatting is a mess. If you didn't indent your PHP properly, that'd be entirely another issue. HTML isn't for people to use, it's for the browser to parse. Now, if I went through your PHP and saw a ton of unnecessary `\n` and `\t`, I'd definitely say your code was a mess. If at the end of your script you used code to beautify your HTML, I'd say you were wasting system resources. – Brad Apr 25 '12 at 23:25
  • @Brad yeah I guess I'll just have to live with it then. thanks – Jose Gomez Apr 25 '12 at 23:41

2 Answers2

2

There are not semantics nor rendering PHP would need to take care about with HTML output in specific.

As far as you're concerned about line-breaks and spaces, in HTML that is called whitespace and it does not have any semantics.

So looks like that everything is already fine. Use your browser's "inspect element" functionality in case you have problems navigating the source-code your own. Or open the source inside a viewer/editor that offers assistance.

hakre
  • 193,403
  • 52
  • 435
  • 836
0

This is really a duplicate of a previous question, however the answers there are worse than you'd get typing 'html beautifier' into Google.

HTML tidy has been around for a long time and is relatively polished. However if your code produces ugly output, then maybe you should fix your code?

Community
  • 1
  • 1
symcbean
  • 47,736
  • 6
  • 59
  • 94