echo or ?><?php

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

    echo or ?><?php

    I have to output a lot of html stuff mixed with php and I'm curious if its
    better to echo the html code or stop the php parser temporarily to got into
    "html mode"? Is there any drawbacks to either method?

    e.g.,

    echo "<div class=\"somecla ss\">"

    or

    ?><div class="someclas s"><?php

    (realize that this is just a simple example and is not meant to represent
    the actual code I will be writing)

    Thanks,
    Jon


  • ZeldorBlat

    #2
    Re: echo or ?&gt;&lt;?php

    On Jun 13, 7:23 pm, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.comwrot e:
    I have to output a lot of html stuff mixed with php and I'm curious if its
    better to echo the html code or stop the php parser temporarily to got into
    "html mode"? Is there any drawbacks to either method?
    >
    e.g.,
    >
    echo "<div class=\"somecla ss\">"
    >
    or
    >
    ?><div class="someclas s"><?php
    >
    (realize that this is just a simple example and is not meant to represent
    the actual code I will be writing)
    >
    Thanks,
    Jon
    In practice it won't really make any noticable difference. Do
    whatever makes the code more readable and easier to maintain.

    Comment

    • SrSilveira

      #3
      Re: echo or ?&gt;&lt;?php

      On Jun 13, 8:23 pm, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.comwrot e:
      I have to output a lot of html stuff mixed with php and I'm curious if its
      better to echo the html code or stop the php parser temporarily to got into
      "html mode"? Is there any drawbacks to either method?
      >
      e.g.,
      >
      echo "<div class=\"somecla ss\">"
      >
      or
      >
      ?><div class="someclas s"><?php
      >
      (realize that this is just a simple example and is not meant to represent
      the actual code I will be writing)
      >
      Thanks,
      Jon
      i think it's better to stop the parser and write the html...
      ....?>
      <div class="someclas s">
      <?=$MyString? >
      </div>
      <?...

      it's easy to see the html and the php code...

      Comment

      • Michael Fesser

        #4
        Re: echo or ?&gt;&lt;?php

        ..oO(SrSilveira )
        >i think it's better to stop the parser and write the html...
        >...?>
        ><div class="someclas s">
        <?=$MyString? >
        ></div>
        ><?...
        Are you sure short_open_tag will be enabled on all of your servers?

        Micha

        Comment

        • Norman Peelman

          #5
          Re: echo or ?&gt;&lt;?php

          Jon Slaughter wrote:
          I have to output a lot of html stuff mixed with php and I'm curious if its
          better to echo the html code or stop the php parser temporarily to got into
          "html mode"? Is there any drawbacks to either method?
          >
          e.g.,
          >
          echo "<div class=\"somecla ss\">"
          >
          or
          >
          ?><div class="someclas s"><?php
          >
          (realize that this is just a simple example and is not meant to represent
          the actual code I will be writing)
          >
          Thanks,
          Jon
          >
          >
          Well, a couple things...

          1) echo "<div class='someclas s'>" is perfectly acceptable afaik as well
          as the reverse

          2) the HEREDOC format also works well, examples:

          $str = <<<EOT
          <HTML>
          <HEAD>
          <TITLE>$pagetit le</TITLE>
          </HEAD>
          <BODY>
          ...
          </BODY>
          </HEAD>
          EOT;

          or

          echo works too, and variables will be resolved following the same rules
          as double quoted strings.

          As to what is better??? Me personally, I don't use escape characters
          or do alot of string concatination at all if I can get away with it...
          too hard on the eyes. I would imagine jumping in and out of the parser
          to be equally hard on the eyes depending on how much you have to do it.
          Again, personally, I use a template system (not Smarty) for that type of
          output. Code the way things are comfortable for you but remember,
          outside the parser no variables can be used.

          Norm

          Comment

          • Jerry Stuckle

            #6
            Re: echo or ?&gt;&lt;?php

            Jon Slaughter wrote:
            I have to output a lot of html stuff mixed with php and I'm curious if its
            better to echo the html code or stop the php parser temporarily to got into
            "html mode"? Is there any drawbacks to either method?
            >
            e.g.,
            >
            echo "<div class=\"somecla ss\">"
            >
            or
            >
            ?><div class="someclas s"><?php
            >
            (realize that this is just a simple example and is not meant to represent
            the actual code I will be writing)
            >
            Thanks,
            Jon
            >
            >
            Jon,

            To me, it all depends. If it's something short, I'll generally just
            echo it. If it's longer, I'll get out of PHP mode.


            As ZedorBlat indicated, you won't notice any real performance impact one
            way or the other. To me, it's all about readability.

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • SrSilveira

              #7
              Re: echo or ?&gt;&lt;?php

              On Jun 13, 10:01 pm, Michael Fesser <neti...@gmx.de wrote:
              .oO(SrSilveira)
              >
              i think it's better to stop the parser and write the html...
              ...?>
              <div class="someclas s">
              <?=$MyString? >
              </div>
              <?...
              >
              Are you sure short_open_tag will be enabled on all of your servers?
              >
              Micha
              of course i'm sure. if it's not enabled you can just do like that
              <?php=$Mystring ?>
              </div>
              <?php...


              Comment

              • Michael Fesser

                #8
                Re: echo or ?&gt;&lt;?php

                ..oO(SrSilveira )
                >On Jun 13, 10:01 pm, Michael Fesser <neti...@gmx.de wrote:
                >
                >Are you sure short_open_tag will be enabled on all of your servers?
                >
                >of course i'm sure. if it's not enabled you can just do like that
                ><?php=$Mystrin g?>
                Still wrong. If you want reliable and portable code, then it has to be

                <?php echo $MyString?>

                short_open_tag is entirely optional. You should only use it if you are
                _always_ in control of the servers your scripts are supposed to run on
                or at least be able to change their configuration. It's still bad style
                and not recommended.

                Micha

                Comment

                • Vladimir Ghetau

                  #9
                  Re: echo or ?&gt;&lt;?php

                  echo "<div class=\"somecla ss\">"
                  >
                  or
                  >
                  ?><div class="someclas s"><?php

                  I would go for


                  echo '<div class="someclas s">

                  <p>'. $some_variable_ here .'</p>

                  </div>';

                  but also, I like the idea of separating the code from the HTML by
                  using templates or simply



                  <div class="someclas s">

                  <p><?php echo $some_variable_ here; ?></p>

                  </div>

                  Both look easy and simply to ....read.


                  Cheers


                  Vladimir

                  Comment

                  • Jeff North

                    #10
                    Re: echo or ?&gt;&lt;?php

                    On Wed, 13 Jun 2007 18:23:42 -0500, in comp.lang.php "Jon Slaughter"
                    <Jon_Slaughter@ Hotmail.com>
                    <V%_bi.18963$C9 6.11122@newssvr 23.news.prodigy .netwrote:
                    >| I have to output a lot of html stuff mixed with php and I'm curious if its
                    >| better to echo the html code or stop the php parser temporarily to got into
                    >| "html mode"? Is there any drawbacks to either method?
                    >|
                    >| e.g.,
                    >|
                    >| echo "<div class=\"somecla ss\">"
                    >|
                    >| or
                    >|
                    >| ?><div class="someclas s"><?php
                    >|
                    >| (realize that this is just a simple example and is not meant to represent
                    >| the actual code I will be writing)
                    I think it would depend upon how you have your file structured.

                    I place most of the php processing at the top of the file an
                    intermingle the necessary php code within the html section.

                    php block
                    html
                    styles block
                    javascript block
                    body
                    <p class="<?php echo $x;?>">some text</p>
                    <select name="lbOptions ">
                    <?php for($x=0; $x<count($recor ds); $x++)
                    echo "<option value='" . $records[$i]->ID . "'>" .
                    $records[$i]->info . "</option>";
                    ?>
                    </select>
                    </body>
                    </html>

                    But then again, when I'm using AJAX calls I usually have the html
                    embedded within the php code.

                    Also, using a text editor with syntax highlighting is a must.
                    ---------------------------------------------------------------
                    jnorthau@yourpa ntsyahoo.com.au : Remove your pants to reply
                    ---------------------------------------------------------------

                    Comment

                    Working...