Question on style

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

    Question on style

    I come from a corporate C background and I find style to be important in
    readability. Especially when multiple people are working on the same code.
    I have seen 3 way to deal with large bocks of static HTML code in PHP. What
    are your preferences? I have listed the techniques in the order of my
    preference.

    Hope I don't start an argument ;)

    Mark C


    Method 1:

    switch ($_POST[Mode])
    {
    case "":
    ?>
    <br><span class='instruct '>Select the area you need to administer.</span>
    <form action="Admin.p hp" method="post">
    <input type="submit" name="Mode" value="User">
    </form>
    <form action="Admin.p hp" method="post">
    <input type="submit" name="Mode" value="Church">
    </form>
    <form action="Admin.p hp" method="post">
    <input type="submit" name="Mode" value="Ministry ">
    </form>
    <?php
    break;

    case "User":
    $hits = GetEMailList($m n[MinistryID]);

    =============== =============== =============== =========

    Method 2:

    switch ($_POST[Mode])
    {
    case "":

    print "<br><span class='instruct '>Select the area you need to
    administer.</span>";
    print "<form action=\"Admin. php\" method=\"post\" >";
    print " <input type=\"submit\" name=\"Mode\" value=\"User\"> ";
    print "</form>";
    print "<form action=\"Admin. php" method="post">" ;
    print " <input type=\"submit" name="Mode" value="Church"> ";
    print "</form>";
    print "<form action=\"Admin. php\" method=\"post\" >";
    print " <input type=\"submit\" name=\"Mode\" value=\"Ministr y\">";
    print "</form>";
    break;

    case "User":
    $hits = GetEMailList($m n[MinistryID]);

    =============== =============== =============== =========

    Method 3:

    switch ($_POST[Mode])
    {
    case "":

    $Var = <<<EOQ
    <br><span class='instruct '>Select the area you need to administer.</span>
    <form action="Admin.p hp" method="post">
    <input type="submit" name="Mode" value="User">
    </form>
    <form action="Admin.p hp" method="post">
    <input type="submit" name="Mode" value="Church">
    </form>
    <form action="Admin.p hp" method="post">
    <input type="submit" name="Mode" value="Ministry ">
    </form>
    EOQ;

    print $Var;
    break;

    case "User":
    $hits = GetEMailList($m n[MinistryID]);


  • Swartz

    #2
    Re: Question on style

    Hi Mark.
    [color=blue]
    > Method 1:
    > switch ($_POST[Mode])
    > {
    > case "":
    > ?>
    > <br><span class='instruct '>Select the area you need to[/color]
    administer.</span>
    <snip...>[color=blue]
    > </form>
    > <?php
    > break;
    >
    > case "User":
    > $hits = GetEMailList($m n[MinistryID]);
    >[/color]


    I'd go with method 1. Main reason for choosing is that its far more
    effecient. Not having to echo/print statid HTML can save ya a lot of CPU
    time.

    Just my $0.02...

    --
    Swartz



    Comment

    • Nikolai Chuvakhin

      #3
      Re: Question on style

      "Mark C" <nospam@today.c om> wrote in message
      news:<104knpthd otn153@corp.sup ernews.com>...[color=blue]
      >
      > I come from a corporate C background[/color]

      Then you probably understand the difference between a constant (Mode)
      and a literal ('Mode') and should stop using $_POST[Mode] and switch
      to $_POST['Mode'].
      [color=blue]
      > and I find style to be important in readability.[/color]

      Unfortunately, readable code is not necessarily the best-performing
      code...
      [color=blue]
      > I have seen 3 way to deal with large bocks of static HTML code in PHP.
      > What are your preferences?[/color]

      None of the three you suggest. It is usually considered a good idea
      to separate logic from content. So my preference would be to define
      the long block of static HTML elsewhere and display it when needed
      via include() or even readfile(). But then, again, that would be
      my personal preference, stemming from aesthetics, not performance
      testing. So here goes:

      Method 4:

      switch ($_POST['Mode']) {
      case '':
      include ('form.php'); // the idea being, HTML code for the form
      echo $form; // is defined as variable $form in form.php
      break;
      case 'User':
      $hits = GetEMailList($m n['MinistryID']);
      }

      Method 5:

      switch ($_POST['Mode']) {
      case '':
      readfile ('form.htm'); // the idea being, HTML code for the form
      // is contained in file form.htm
      break;
      case 'User':
      $hits = GetEMailList($m n['MinistryID']);
      }

      It is also usually considered a good idea to conceal form.* file
      by putting it into a directory outside the document root or into
      a restricted-access directory protected with .htaccess.

      Cheers,
      NC

      Comment

      • Hayden Kirk

        #4
        Re: Question on style

        Yes...

        I have been using method 1 for along time and I dont like it at all. Its a
        pig of a way to do things. Including the html is much better. You can store
        "hook" in the html so it can be a bit dynamic. EG <title>%TITLE %</title>.

        I find this is alot better to work with, you can edit the html in an editor
        like Dreamweaver and use PHPed or something similer to edit the php...

        I also use a class called page that renders a html page... maybe something
        else to think about?

        Just my 0.02c :P

        "Nikolai Chuvakhin" <nc@iname.com > wrote in message
        news:32d7a63c.0 403061823.2b4d7 dbe@posting.goo gle.com...[color=blue]
        > "Mark C" <nospam@today.c om> wrote in message
        > news:<104knpthd otn153@corp.sup ernews.com>...[color=green]
        > >
        > > I come from a corporate C background[/color]
        >
        > Then you probably understand the difference between a constant (Mode)
        > and a literal ('Mode') and should stop using $_POST[Mode] and switch
        > to $_POST['Mode'].
        >[color=green]
        > > and I find style to be important in readability.[/color]
        >
        > Unfortunately, readable code is not necessarily the best-performing
        > code...
        >[color=green]
        > > I have seen 3 way to deal with large bocks of static HTML code in PHP.
        > > What are your preferences?[/color]
        >
        > None of the three you suggest. It is usually considered a good idea
        > to separate logic from content. So my preference would be to define
        > the long block of static HTML elsewhere and display it when needed
        > via include() or even readfile(). But then, again, that would be
        > my personal preference, stemming from aesthetics, not performance
        > testing. So here goes:
        >
        > Method 4:
        >
        > switch ($_POST['Mode']) {
        > case '':
        > include ('form.php'); // the idea being, HTML code for the form
        > echo $form; // is defined as variable $form in form.php
        > break;
        > case 'User':
        > $hits = GetEMailList($m n['MinistryID']);
        > }
        >
        > Method 5:
        >
        > switch ($_POST['Mode']) {
        > case '':
        > readfile ('form.htm'); // the idea being, HTML code for the form
        > // is contained in file form.htm
        > break;
        > case 'User':
        > $hits = GetEMailList($m n['MinistryID']);
        > }
        >
        > It is also usually considered a good idea to conceal form.* file
        > by putting it into a directory outside the document root or into
        > a restricted-access directory protected with .htaccess.
        >
        > Cheers,
        > NC[/color]


        Comment

        • Mark C

          #5
          Re: Question on style


          "Nikolai Chuvakhin" <nc@iname.com > wrote in message
          news:32d7a63c.0 403061823.2b4d7 dbe@posting.goo gle.com...[color=blue]
          > "Mark C" <nospam@today.c om> wrote in message
          > news:<104knpthd otn153@corp.sup ernews.com>...[color=green]
          > >
          > > I come from a corporate C background[/color]
          >
          > Then you probably understand the difference between a constant (Mode)
          > and a literal ('Mode') and should stop using $_POST[Mode] and switch
          > to $_POST['Mode'].[/color]

          I had seen the different syntax, but none of the texts I have indicated the
          difference. Sinice it worked either way I assumed there was no difference.
          But as I think about all the thing you can put in the [] I see how the ' can
          help the interpterter.

          [color=blue]
          >[color=green]
          > > I have seen 3 way to deal with large bocks of static HTML code in PHP.
          > > What are your preferences?[/color]
          >
          > None of the three you suggest. It is usually considered a good idea
          > to separate logic from content. So my preference would be to define
          > the long block of static HTML elsewhere and display it when needed
          > via include() or even readfile(). But then, again, that would be
          > my personal preference, stemming from aesthetics, not performance
          > testing. So here goes:
          >
          > Method 4:
          >
          > switch ($_POST['Mode']) {
          > case '':
          > include ('form.php'); // the idea being, HTML code for the form
          > echo $form; // is defined as variable $form in form.php
          > break;
          > case 'User':
          > $hits = GetEMailList($m n['MinistryID']);
          > }
          >
          > Method 5:
          >
          > switch ($_POST['Mode']) {
          > case '':
          > readfile ('form.htm'); // the idea being, HTML code for the form
          > // is contained in file form.htm
          > break;
          > case 'User':
          > $hits = GetEMailList($m n['MinistryID']);
          > }[/color]

          I like both these ideas. I did not think of either right off the top of my
          head. I would have to get used to looking to another document for content.
          I generaly try to put logic in functions and classes and keep the main page
          more output oriatned with calls to libraries for number crunching.

          Good discussion,

          Mark C


          Comment

          • CountScubula

            #6
            Re: Question on style

            "Mark C" <nospam@today.c om> wrote in message
            news:104knpthdo tn153@corp.supe rnews.com...[color=blue]
            > I come from a corporate C background and I find style to be important in
            > readability. Especially when multiple people are working on the same[/color]
            code.[color=blue]
            > I have seen 3 way to deal with large bocks of static HTML code in PHP.[/color]
            What[color=blue]
            > are your preferences? I have listed the techniques in the order of my
            > preference.
            >
            > Hope I don't start an argument ;)
            >
            > Mark C
            >
            >
            > Method 1:
            >
            > switch ($_POST[Mode])
            > {
            > case "":
            > ?>
            > <br><span class='instruct '>Select the area you need to[/color]
            administer.</span>[color=blue]
            > <form action="Admin.p hp" method="post">
            > <input type="submit" name="Mode" value="User">
            > </form>
            > <form action="Admin.p hp" method="post">
            > <input type="submit" name="Mode" value="Church">
            > </form>
            > <form action="Admin.p hp" method="post">
            > <input type="submit" name="Mode" value="Ministry ">
            > </form>
            > <?php
            > break;
            >
            > case "User":
            > $hits = GetEMailList($m n[MinistryID]);
            >
            > =============== =============== =============== =========
            >
            > Method 2:
            >
            > switch ($_POST[Mode])
            > {
            > case "":
            >
            > print "<br><span class='instruct '>Select the area you need to
            > administer.</span>";
            > print "<form action=\"Admin. php\" method=\"post\" >";
            > print " <input type=\"submit\" name=\"Mode\" value=\"User\"> ";
            > print "</form>";
            > print "<form action=\"Admin. php" method="post">" ;
            > print " <input type=\"submit" name="Mode" value="Church"> ";
            > print "</form>";
            > print "<form action=\"Admin. php\" method=\"post\" >";
            > print " <input type=\"submit\" name=\"Mode\" value=\"Ministr y\">";
            > print "</form>";
            > break;
            >
            > case "User":
            > $hits = GetEMailList($m n[MinistryID]);
            >
            > =============== =============== =============== =========
            >
            > Method 3:
            >
            > switch ($_POST[Mode])
            > {
            > case "":
            >
            > $Var = <<<EOQ
            > <br><span class='instruct '>Select the area you need to[/color]
            administer.</span>[color=blue]
            > <form action="Admin.p hp" method="post">
            > <input type="submit" name="Mode" value="User">
            > </form>
            > <form action="Admin.p hp" method="post">
            > <input type="submit" name="Mode" value="Church">
            > </form>
            > <form action="Admin.p hp" method="post">
            > <input type="submit" name="Mode" value="Ministry ">
            > </form>
            > EOQ;
            >
            > print $Var;
            > break;
            >
            > case "User":
            > $hits = GetEMailList($m n[MinistryID]);
            >
            >[/color]

            Well, you know what they say about opionion, they are like... well, lets
            just say everyone has one. :)

            I can warrent times when you would need to do all the 3 you listed above.
            method 1: if you needed to edit the html section in say, dreamweaver.
            method 2: if you jsut need to spit out a couple lines
            method 3: if the block has vars in it, but I would do print <<< instead of
            $var <<<
            method 4: method 1, as was stated by someone else, put html in a seperate
            file, use to keep site very dynamic

            just my 1 cent, need other 1 cent for another post

            --
            Mike Bradley
            http://www.gzentools.com -- free online php tools


            Comment

            • Pedro Graca

              #7
              Re: Question on style

              Mark C wrote:[color=blue]
              >
              > "Nikolai Chuvakhin" <nc@iname.com > wrote in message
              > news:32d7a63c.0 403061823.2b4d7 dbe@posting.goo gle.com...[/color]
              [color=blue][color=green]
              >> Then you probably understand the difference between a constant (Mode)
              >> and a literal ('Mode') and should stop using $_POST[Mode] and switch
              >> to $_POST['Mode'].[/color]
              >
              > I had seen the different syntax, but none of the texts I have indicated the
              > difference. Sinice it worked either way I assumed there was no difference.
              > But as I think about all the thing you can put in the [] I see how the ' can
              > help the interpterter.[/color]

              If you enable all errors, warnings, and notices (as you should, at
              least, while developing the script) you'll get a notice for using
              undefined constants.

              <?php
              error_reporting (E_ALL);

              define('Mode2', 'something');

              echo 'Mode'; // OK
              echo Mode; // Notice: Use of undefined constant Mode - assumed 'Mode'

              echo 'Mode2'; // OK
              echo Mode2; // OK; prints "something"
              ?>
              --
              --= my mail box only accepts =--
              --= Content-Type: text/plain =--
              --= Size below 10001 bytes =--

              Comment

              Working...