Quick PHP Question

Associate
Joined
26 Jun 2003
Posts
1,140
Location
North West
Im writing a class do do some mysql stuff then print out results in a table.

Is there anyway to print the resulting html code out in a well formatted way?

Code:
function test()
{
  $returnThis = "<table>".
                "  <thead>".
                "    <tr>".
                "      <th>Header 1/th>".
                "      <th>Header 2</th>".
                "      <th>Header 2</th>".
                "    </tr>".
                "  </thead>".
                "</table>";
  return $returnThis;
}

echo test();

However the above html prints it all out in one line. I know its no biggy but I like to see nice html code.
 
Im writing a class do do some mysql stuff then print out results in a table.

Is there anyway to print the resulting html code out in a well formatted way?


However the above html prints it all out in one line. I know its no biggy but I like to see nice html code.

Add a newline "\n" ;)

Code:
function test()
{
  $returnThis = "<table>\n".
                "  <thead>\n".
                "    <tr>\n".
                "      <th>Header 1/th>\n".
                "      <th>Header 2</th>\n".
                "      <th>Header 2</th>\n".
                "    </tr>\n".
                "  </thead>\n".
                "</table>\n";
  return $returnThis;
}

echo test();
 
Indentation is just to keep it readable when you're writing it - if you're using PHP to generate the html, then why worry about how it's indented?
 
Haha thats a way i didnt think of. lol Cheers.

However tho, if you use the code like this:

Code:
...
  <body>
    <div id="wrapper">
      <?=$myClass->getTable()?>
    </div>
  </body>
...

Is printed out

Code:
  <body>
    <div id="wrapper">
      <table>
  <thead>
    <tr>
      <th>Header 1</th>
      <th>Header 2</th>
      <th>Header 2</th>
    </tr>
  </thead>
</table>
    </div>
  </body>

As you can see it is tidyier, however only the opening <table> tag is indented properly.

Any way this can be sorted? :D

Sorry im picky :p
 
Indentation is just to keep it readable when you're writing it - if you're using PHP to generate the html, then why worry about how it's indented?

Not really a big reason, I just like to read nicely laid out html when I need to look through the source.
 
The way I tend to do it is to run it through the Tidy PHP extension before displaying when developing / debugging my html / javascript. Then just outputting it normally when in production.
 
can I ask why you're concatenating those strings? seems rather pointless to me.
Code:
function test()
{
  return 
"<table>
  <thead>
    <tr>
      <th>Header 1/th>
      <th>Header 2</th>
      <th>Header 2</th>
    </tr>
  </thead>
</table>";
}

echo test();

needing your HTML indented like this makes for messy, confusing code in the long run. It's very worthwhile reading into generating HTML with the DOM in PHP.
 
just use tidy tidy if you are such a perfectionist with the way your code looks (like me). worth the extra overhead in most cases imo :)

If you're running a high-traffic site then you'd be better off with no whitespace at all to save on bandwidth. If you want to read the code, just paste it into an editor with a html source format function.
 
Back
Top Bottom