Is it better to echo or leave as html?

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

    Is it better to echo or leave as html?

    I have a multi step signup form that I'm building and I have the
    following question. Should I do it this way:

    <?php

    if (blah == 1) {
    ?><table><td width="100">... .....etc
    <?php } else { ?>
    <table><td width="100">... .....etc
    <?php } ?>

    OR

    <?php

    if (blah == 1) {
    echo "<table><td width=\"100"\>. .......etc";
    } else
    echo "<table><td width=\"100"\>. .......etc";
    }?>




    If the second way is better is there an easier way to convert the HTML
    rather then writting echo a million times by hand?





  • Andy Hassall

    #2
    Re: Is it better to echo or leave as html?

    On 30 Sep 2003 17:24:17 -0500, Nick Messick <nospam@trendwh ore.com> wrote:
    [color=blue]
    >I have a multi step signup form that I'm building and I have the
    >following question. Should I do it this way:
    >
    ><?php
    >
    >if (blah == 1) {
    >?><table><td width="100">... .....etc
    ><?php } else { ?>
    ><table><td width="100">... .....etc
    ><?php } ?>
    >
    >OR
    >
    ><?php
    >
    >if (blah == 1) {
    > echo "<table><td width=\"100"\>. .......etc";
    >} else
    > echo "<table><td width=\"100"\>. .......etc";
    >}?>[/color]

    It's a matter of personal preference, and depends on what you're outputting.

    I prefer the first one; drop out of php and output literal HTML, unless it's a
    very short output within a much greater number of lines of PHP.

    I think an improvement on the second option is instead of:

    echo "<table><td width=\"100"\>. .......etc";

    Use single quotes in the HTML inside:

    echo "<table><td width='100'>... .....etc";

    This reduces the number of slashes, making it much easier to read.

    --
    Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
    Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

    Comment

    • dr. zoidberg

      #3
      Re: Is it better to echo or leave as html?

      Andy Hassall wrote:
      [color=blue]
      > Use single quotes in the HTML inside:
      >
      > echo "<table><td width='100'>... .....etc";
      >
      > This reduces the number of slashes, making it much easier to read.[/color]

      i'm not sure about this. It will reduce number of slashes, but will it
      be W3C compliant?





      Comment

      • Andy Hassall

        #4
        Re: Is it better to echo or leave as html?

        On Wed, 01 Oct 2003 01:34:39 +0200, "dr. zoidberg" <someone@exampl e.wrong>
        wrote:
        [color=blue]
        >Andy Hassall wrote:
        >[color=green]
        >> Use single quotes in the HTML inside:
        >>
        >> echo "<table><td width='100'>... .....etc";
        >>
        >> This reduces the number of slashes, making it much easier to read.[/color]
        >
        >i'm not sure about this. It will reduce number of slashes, but will it
        >be W3C compliant?[/color]

        Yes.



        "By default, SGML requires that all attribute values be delimited using either
        double quotation marks (ASCII decimal 34) or single quotation marks (ASCII
        decimal 39)."

        --
        Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
        Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

        Comment

        • rush

          #5
          Re: Is it better to echo or leave as html?

          "Nick Messick" <nospam@trendwh ore.com> wrote in message
          news:3f7a01dd$0 $71498$45beb828 @newscene.com.. .[color=blue]
          > I have a multi step signup form that I'm building and I have the
          > following question. Should I do it this way:[/color]

          There are people who prefere it first way, and there are people who prefere
          it second way. I personaly prefere it when html and code are in completely
          separate files.

          rush
          --




          Comment

          • Jean-Marc Molina

            #6
            Re: Is it better to echo or leave as html?

            Hi Rush,
            [color=blue]
            > I personaly prefere it when html and code are in completely separate[/color]
            files.

            Don't forget that a Template Engine is not supposed to separate PHP code
            from HTML but business logic from presentation logic.

            Just to evangelize you : http://www.sitepoint.com/article/1218

            JM ~ an angel passes by ~

            --
            Clé AntiPourriel : PASUNPOURRIEL (ne pas retirer)


            Comment

            • Jean-Marc Molina

              #7
              Re: Is it better to echo or leave as html?

              First way. Because embedding HTML in a echo is a real pain.

              From the manual : "PHP is scripting language that is especially suited for
              Web development and can be embedded into HTML"

              "embedded", it's the magic word. It means PHP is meant for that, you need
              PHP, use <?php Some PHP code... ?>, you don't it anymore ? Escape from PHP
              to HTML : ?>Some HTML...<?php... You just need to output the value of a PHP
              variable ? Use the echo shortcut : <?= $my_php_var ?>... And voilà :)

              Hope it helps,
              JM

              --
              Clé AntiPourriel : PASUNPOURRIEL (ne pas retirer)


              Comment

              • rush

                #8
                Re: Is it better to echo or leave as html?

                "Jean-Marc Molina" <goa_pasdepourr iel_@ifrance.co m> wrote in message
                news:blevkt$2bf $1@news-reader5.wanadoo .fr...[color=blue]
                > Hi Rush,
                >[color=green]
                > > I personaly prefere it when html and code are in completely separate[/color]
                > files.
                >
                > Don't forget that a Template Engine is not supposed to separate PHP code
                > from HTML but business logic from presentation logic.[/color]

                There are different school of thoughts on this, and I do not prefer the one
                you propose, while I accept that it works for you and that you prefer it.

                I think Template engine purpose _is_ to separate html from code. And that
                bussiness logic shoud be separated from presentation logic by organizing
                your code in MVC, or MVP patterns where business logic goes into M. But it
                is the code part that separates presentation logic and bussines, not
                templates which are used to separate html which define pure visial
                appearance, and code defines presentation logic. It is much clear in
                languages that come with MVP out of the box, like Dolphin Smalltalk. There
                you have View composer, where you paint the appearance of the GUI and it
                gets stored as a resource. It is equivalent of html. Than there is
                Presenter, which is pure code and contains "Presentati on" logic. And there
                is Model that contains bussines logic.

                The idea that template purpose is to separate business logic from
                presentation has lead to "dirty" templates which contain both code and html.
                In this scenario code (not necessary a php code, but code with loops, ifs
                and other constructs anyway), is embeded into the html template. Most
                prominent and respected examples of such approach would be XML+XSLT and
                Smarty.

                Again, both approaches work and some people like one, and some people like
                onother, but it is by no means such a clear cut, and just a single "proper"
                way to approach things.

                Thanks,

                rush
                --




                Comment

                • Martin Lucas-Smith

                  #9
                  Re: Is it better to echo or leave as html?



                  [color=blue]
                  > Use single quotes in the HTML inside:
                  >
                  > echo "<table><td width='100'>... .....etc";
                  >
                  > This reduces the number of slashes, making it much easier to read.[/color]

                  Or rather, if (like myself) you don't like using ' for quoting attributes
                  in HTML, use:

                  echo '<table><td width="100">... .....etc'


                  This only becomes a problem if this needs to contain a variable, in which
                  case you do:

                  echo '<table><td width="100">' . $variable . '......etc'



                  Double-quotes in PHP mean that PHP parses (looks through) the quoted
                  string for variales and PHP-specific things like \n or \r

                  Single quotes doesn't - it just dumps the result back, and so is
                  theoretically quicker.

                  This applies in all sorts of cases, and the PHP documentation often uses "
                  even when not necessary.



                  You should also consider doing this instead of echoing:

                  $html = "\n" . '<table><td width="100">'
                  $html .= "\n<tr>";
                  #etc
                  $html .= "\n</table>";

                  echo $html;


                  I.e. build up your HTML then when it's complete, show it. This has the
                  benefit of not producing half-complete HTML if your script finds a problem
                  half-way through. It also makes printing error messages more flexible,
                  because at the end you could have a check which then overwrites the
                  built-up HTML with the warning(s), etc. - if you've already echo'd the
                  result, you don't have that choice, and you have to do *all* the checking
                  at the start.


                  Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22



                  Comment

                  Working...