PHP to echo a line in html source...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Domestos

    PHP to echo a line in html source...

    Here an unusual one...

    Say i am writing a few lines of code in php script as so...

    <?php
    echo '<table>';
    echo '<tr>';
    echo '<td> blah blah </td>';
    echo '</tr>';
    echo '</table>';
    ?>

    On my html source the code looks like this...

    <table><tr><td> blah blah</td></tr></table>

    Is there anyway I can get each html tag on a seperate line in the html
    source so it is nice and neat (easy readable)?

    Thanks
    Domestos



  • Andy Hassall

    #2
    Re: PHP to echo a line in html source...

    On Tue, 23 Aug 2005 20:24:38 GMT, "Domestos" <never.you@mind .net> wrote:
    [color=blue]
    >Here an unusual one...
    >
    >Say i am writing a few lines of code in php script as so...
    >
    ><?php
    >echo '<table>';
    >echo '<tr>';
    >echo '<td> blah blah </td>';
    >echo '</tr>';
    >echo '</table>';
    >?>
    >
    >On my html source the code looks like this...
    >
    ><table><tr><td >blah blah</td></tr></table>
    >
    >Is there anyway I can get each html tag on a seperate line in the html
    >source so it is nice and neat (easy readable)?[/color]

    echo "<table>\n" ;

    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • JDS

      #3
      Re: PHP to echo a line in html source...

      On Tue, 23 Aug 2005 21:36:30 +0100, Andy Hassall wrote:
      [color=blue]
      > echo "<table>\n" ;[/color]

      Let me also point out that Andy used double-quotes. Doing this:

      echo '<table>\n';

      will not produce the same output.

      I recommend that you not echo, line by line, large blocks of HTML like you
      have done. Makes it quite hard to read (and debug or alter or update).

      --
      JDS | jeffrey@example .invalid
      | http://www.newtnotes.com
      DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

      Comment

      • Ken Robinson

        #4
        Re: PHP to echo a line in html source...

        Domestos wrote:[color=blue]
        > Here an unusual one...
        >
        > Say i am writing a few lines of code in php script as so...
        >
        > <?php
        > echo '<table>';
        > echo '<tr>';
        > echo '<td> blah blah </td>';
        > echo '</tr>';
        > echo '</table>';
        > ?>
        >
        > On my html source the code looks like this...
        >
        > <table><tr><td> blah blah</td></tr></table>
        >
        > Is there anyway I can get each html tag on a seperate line in the html
        > source so it is nice and neat (easy readable)?[/color]

        How about:

        <?php
        $tmp = array();
        $tmp[] = '<table>';
        $tmp[] = "\t".'<tr>' ;
        $tmp[] = "\t\t".'<td > blah blah </td>';
        $tmp[] = "\t".'<tr>' ;
        $tmp[] = '</table>';
        echo implode("\n",$t mp)."\n";
        ?>

        I use similar constructs all the time. It make for nice readable code.

        Ken

        Comment

        • Hilarion

          #5
          Re: PHP to echo a line in html source...

          > [snip][color=blue]
          > <?php
          > echo '<table>';
          > echo '<tr>';
          > echo '<td> blah blah </td>';
          > echo '</tr>';
          > echo '</table>';
          > ?>
          >
          > [snip]
          >
          > Is there anyway I can get each html tag on a seperate line in the html
          > source so it is nice and neat (easy readable)?[/color]

          Many ways.
          First one (using double quotes and escape sequence \n):

          <?php
          echo "<table>\n" ;
          echo "<tr>\n";
          echo "<td> blah blah </td>\n";
          echo "</tr>\n";
          echo "</table>\n";
          ?>

          or:

          <?php
          echo "<table>\n<tr>\ n<td> blah blah </td>\n</tr>\n</table>\n";
          ?>

          or:

          <?php
          echo '<table>'."\n" ;
          echo '<tr>'."\n";
          echo '<td> blah blah </td>'."\n";
          echo '</tr>'."\n";
          echo '</table>'."\n";
          ?>

          or escaping to HTML:

          <?php
          // some PHP code
          ?>
          <table>
          <tr>
          <td> blah blah </td>
          </tr>
          </table>
          <?php
          // other PHP code
          ?>

          or using multiline strings (and double quotes):

          <?php
          echo "<table>
          <tr>
          <td> blah blah </td>
          </tr>
          </table>
          ";
          ?>

          or (probably) many more.


          Hilarion

          Comment

          • Domestos

            #6
            Re: PHP to echo a line in html source...

            I suppouse to understand them I need to understand the significance between
            doubles quotes and single quotes in php code...

            Can any one help? or point me to some text

            thanks for your help so far, the hardest part is choosing which one to use
            and stick too...

            Andy Mak



            Comment

            • Andy Hassall

              #7
              Re: PHP to echo a line in html source...

              On Tue, 23 Aug 2005 20:59:51 GMT, "Domestos" <never.you@mind .net> wrote:
              [color=blue]
              >I suppouse to understand them I need to understand the significance between
              >doubles quotes and single quotes in php code...[/color]



              --
              Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
              http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

              Comment

              • Domestos

                #8
                Re: PHP to echo a line in html source...

                [color=blue]
                > or using multiline strings (and double quotes):
                >
                > <?php
                > echo "<table>
                > <tr>
                > <td> blah blah </td>
                > </tr>
                > </table>
                > ";
                > ?>[/color]

                <snip>

                That one thats worked for me is as follows... multiline string, with single
                quotes. I needed to embed php variables into the string also... as
                follows...
                echo '<table border="0" cellspacing="0" cellpadding="0" >
                <tr>
                <td><img src="gfx/interface/win_'.$wincol.' _tl.gif" width="10" height="10"
                border="0"></td>
                <td><img src="gfx/interface/win_'.$wincol.' _t.gif" width="'.$winwi d.'"
                height="10" border="0"></td>
                <td><img src="gfx/interface/win_'.$wincol.' _tr.gif" width="10" height="10"
                border="0"></td>
                </tr>
                <tr>
                <td><img src="gfx/interface/win_'.$wincol.' _l.gif" width="10"
                height="'.$winh gt.'" border="0"></td>
                <td width="'.$winwi d.'" height="'.$winh gt.'" bgcolor="#39393 9">';


                Comment

                • Will Woodhull

                  #9
                  Re: PHP to echo a line in html source...

                  Domestos wrote:
                  <snip>[color=blue]
                  > Is there anyway I can get each html tag on a seperate line in the html
                  > source so it is nice and neat (easy readable)?[/color]


                  My favorite for large blocks of HTML hasn't been mentioned yet. It is
                  the "here document" structure for defining a string:

                  <?php
                  $something = "blah blah"; # just to show off variable interpolation

                  echo <<<EOHEREDOC
                  <table>
                  <tr>
                  <td> $something </td>
                  </tr>
                  </table>
                  EOHEREDOC;

                  ?>

                  Check out 'PHP "Here document" tutorial' with Google. Unfortunately
                  this form of defining a string is not covered very well in the PHP
                  manual.

                  The here document preserves whitespace and newlines, and interpolates
                  variables in the same way doublequote strings do.

                  Comment

                  • Oliver Grätz

                    #10
                    Re: PHP to echo a line in html source...

                    Domestos schrieb:[color=blue]
                    > <table><tr><td> blah blah</td></tr></table>
                    >
                    > Is there anyway I can get each html tag on a seperate line in the html
                    > source so it is nice and neat (easy readable)?[/color]

                    Well, you CAN try to make it nice by yourself, but there's also the
                    possibility of letting software do that job. With "tidy" one can
                    prettyprint HTML code and there are possibilities do automagically use
                    it when sending HTML to the browser. There are extension to PHP that
                    allow for this. You should read a bit about using the output buffers or
                    the tidy extension.

                    As an appetizer, just have a look at this comment:


                    AllOLLi

                    Comment

                    Working...