"Best" coding practice?

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

    "Best" coding practice?

    As an amateur wannabe-pro programmer, I am trying to learn not only how to
    use PHP but how to do it *efficiently*. (Trust me, you don't wanna see some
    of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
    and PHP, very interspersed. Example: A form with 20 fields where I echo a
    variable in each field to show existing data. My question is really about
    the best practice to do this, as I have seen some interesting use of
    includes/etc.

    Is it "better" to replicate my form in HTML and insert <?PHP echo $variable;
    ?> in each tag?
    Or would it be better to somehow put all my PHP up front and echo the HTML
    at one time?

    BTW, I have this same debate on a couple of pages where I do mySQL queries
    2-3 times on the same page... Advice, suggestions and recommendations are
    greatly appreciated!

    Thanx,
    Wm




  • Floris van den Berg

    #2
    Re: &quot;Best&quot ; coding practice?

    "Wm" <LAshooter@hotm ail.com> schreef in bericht
    news:EaYlb.3518 740$Bf5.483767@ news.easynews.c om...[color=blue]
    > As an amateur wannabe-pro programmer, I am trying to learn not only how to
    > use PHP but how to do it *efficiently*. (Trust me, you don't wanna see[/color]
    some[color=blue]
    > of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
    > and PHP, very interspersed. Example: A form with 20 fields where I echo a
    > variable in each field to show existing data. My question is really about
    > the best practice to do this, as I have seen some interesting use of
    > includes/etc.
    >
    > Is it "better" to replicate my form in HTML and insert <?PHP echo[/color]
    $variable;[color=blue]
    > ?> in each tag?
    > Or would it be better to somehow put all my PHP up front and echo the HTML
    > at one time?[/color]

    Personally i prefer to have one big block of php code, and echoes to print
    HTML.
    [color=blue]
    > BTW, I have this same debate on a couple of pages where I do mySQL queries
    > 2-3 times on the same page... Advice, suggestions and recommendations are
    > greatly appreciated![/color]

    I seperated my database query stuff from the rest of the code by putting
    that code in classes and in seperate php files. I then include_once those
    files. The major reason why i decided to work like this, is that when i
    decide to change some things in the structure of my database, most of my
    code won't have to change. Only those classes using that particular bit of
    database.

    Floris



    Comment

    • timmeh

      #3
      Re: &quot;Best&quot ; coding practice?

      You should really look into templating.. There's a nice templating class
      called phpLibs; I personally use the phpBB forums template class, works
      incredibly well and it's very easy to use.. Keeping your code and html
      separate makes it much easier to read and modify..

      Regards,
      Tim


      "Wm" <LAshooter@hotm ail.com> wrote in message
      news:EaYlb.3518 740$Bf5.483767@ news.easynews.c om...[color=blue]
      > As an amateur wannabe-pro programmer, I am trying to learn not only how to
      > use PHP but how to do it *efficiently*. (Trust me, you don't wanna see[/color]
      some[color=blue]
      > of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
      > and PHP, very interspersed. Example: A form with 20 fields where I echo a
      > variable in each field to show existing data. My question is really about
      > the best practice to do this, as I have seen some interesting use of
      > includes/etc.
      >
      > Is it "better" to replicate my form in HTML and insert <?PHP echo[/color]
      $variable;[color=blue]
      > ?> in each tag?
      > Or would it be better to somehow put all my PHP up front and echo the HTML
      > at one time?
      >
      > BTW, I have this same debate on a couple of pages where I do mySQL queries
      > 2-3 times on the same page... Advice, suggestions and recommendations are
      > greatly appreciated!
      >
      > Thanx,
      > Wm
      >
      >
      >
      >[/color]


      Comment

      • Louis-Philippe Huberdeau

        #4
        Re: &quot;Best&quot ; coding practice?

        The goal is not to seperate HTML and PHP, it's to seperate display logic
        from 'buisness logic'. Using a loop inside your HTML is not a bad thing,
        as long as you don't fetch your data in there.

        Adding a templating engine above PHP is not always necessary. PHP can be
        used as a templating engine itself if you don't use every feature of the
        language, and it can be quite simple, even for a non-programmer.

        timmeh wrote:[color=blue]
        > You should really look into templating.. There's a nice templating class
        > called phpLibs; I personally use the phpBB forums template class, works
        > incredibly well and it's very easy to use.. Keeping your code and html
        > separate makes it much easier to read and modify..
        >
        > Regards,
        > Tim
        >
        >
        > "Wm" <LAshooter@hotm ail.com> wrote in message
        > news:EaYlb.3518 740$Bf5.483767@ news.easynews.c om...
        >[color=green]
        >>As an amateur wannabe-pro programmer, I am trying to learn not only how to
        >>use PHP but how to do it *efficiently*. (Trust me, you don't wanna see[/color]
        >
        > some
        >[color=green]
        >>of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
        >>and PHP, very interspersed. Example: A form with 20 fields where I echo a
        >>variable in each field to show existing data. My question is really about
        >>the best practice to do this, as I have seen some interesting use of
        >>includes/etc.
        >>
        >>Is it "better" to replicate my form in HTML and insert <?PHP echo[/color]
        >
        > $variable;
        >[color=green]
        >>?> in each tag?
        >>Or would it be better to somehow put all my PHP up front and echo the HTML
        >>at one time?
        >>
        >>BTW, I have this same debate on a couple of pages where I do mySQL queries
        >>2-3 times on the same page... Advice, suggestions and recommendations are
        >>greatly appreciated!
        >>
        >>Thanx,
        >>Wm
        >>
        >>
        >>
        >>[/color]
        >
        >
        >[/color]

        Comment

        • rush

          #5
          Re: &quot;Best&quot ; coding practice?

          "Wm" <LAshooter@hotm ail.com> wrote in message
          news:EaYlb.3518 740$Bf5.483767@ news.easynews.c om...[color=blue]
          > As an amateur wannabe-pro programmer, I am trying to learn not only how to
          > use PHP but how to do it *efficiently*. (Trust me, you don't wanna see[/color]
          some[color=blue]
          > of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
          > and PHP, very interspersed. Example: A form with 20 fields where I echo a
          > variable in each field to show existing data. My question is really about
          > the best practice to do this, as I have seen some interesting use of
          > includes/etc.[/color]

          Hi,

          take a look at TemplateTamer, it is IDE and template engine, that let's you
          keep html and php in completely separate files. On related wiki you will
          find several examples.

          rush
          --
          Get your very own domain easily. Fast and professional customer service.




          Comment

          • rush

            #6
            Re: &quot;Best&quot ; coding practice?

            "Louis-Philippe Huberdeau" <lphuberdeau@sy mpatico.ca> wrote in message
            news:Vm0mb.7916 $7t3.322200@new s20.bellglobal. com...[color=blue]
            > The goal is not to seperate HTML and PHP, it's to seperate display logic
            > from 'buisness logic'. Using a loop inside your HTML is not a bad thing,
            > as long as you don't fetch your data in there.[/color]

            Well, there are at least 2 way to look at that problem.

            My prefered approach is to use template engine to separate html and php, and
            MVC to separate presentation logic and bussines logic. So that are 2
            separate problems, and stuffing too much into template in order to handle
            whole presentation layer makes it fat (or dirty if you will), for my taste.
            I prefer template to remain just an resource with markers, no form of
            controls structures in it, and control structures should be in separate php
            file.

            But I guess it is also a matter of personal preferences, and one should use
            approah that fits him the best.

            rush
            --





            Comment

            • Dave Bell

              #7
              Re: &quot;Best&quot ; coding practice?

              I always use herdoc string output embedded into PHP, like this

              <?php

              .... code
              .... code
              .... code

              print <<<BLOCKNAME

              <html><head><ti tle>
              <img src="thisone">
              <img src="{$fromthed atabase}">

              Hello, {$name}, this is my lovely website.

              BLOCKNAME;

              You get all the advantages of double qoutes, but none of the hassle of
              escaping quotes in HTML. Its the best form of string output when
              creating large areas of HTML to be sent.

              Comment

              • David Quinton

                #8
                Re: &quot;Best&quot ; coding practice?

                On Fri, 24 Oct 2003 09:30:29 +0100, Dave Bell <drb8@kent.ac.u k> wrote:
                [color=blue]
                >I always use herdoc string output embedded into PHP, like this[/color]

                Wow - never seen that before.
                Is it like printqq in Perl?
                --
                Games, Gizmos, Gifts and Toys from 'I want one of those'
                <http://www.bizorg.co.u k/shopping/>

                Comment

                • s a n j a y

                  #9
                  Re: &quot;Best&quot ; coding practice?

                  EXactly ! Thats my favourite feture too.

                  sanjay

                  "Dave Bell" <drb8@kent.ac.u k> wrote in message
                  news:bnant0$itc $2@athena.ukc.a c.uk...
                  | I always use herdoc string output embedded into PHP, like this
                  |
                  | <?php
                  |
                  | ... code
                  | ... code
                  | ... code
                  |
                  | print <<<BLOCKNAME
                  |
                  | <html><head><ti tle>
                  | <img src="thisone">
                  | <img src="{$fromthed atabase}">
                  |
                  | Hello, {$name}, this is my lovely website.
                  |
                  | BLOCKNAME;
                  |
                  | You get all the advantages of double qoutes, but none of the hassle of
                  | escaping quotes in HTML. Its the best form of string output when
                  | creating large areas of HTML to be sent.
                  |


                  Comment

                  • Michael Vester

                    #10
                    Re: &quot;Best&quot ; coding practice?

                    Wm wrote:
                    [color=blue]
                    > As an amateur wannabe-pro programmer, I am trying to learn not only
                    > how to
                    > use PHP but how to do it *efficiently*. (Trust me, you don't wanna
                    > see some
                    > of my stuff!!!) I'm noticing a number of my pages have a mixture of
                    > HTML and PHP, very interspersed. Example: A form with 20 fields where
                    > I echo a
                    > variable in each field to show existing data. My question is really
                    > about the best practice to do this, as I have seen some interesting
                    > use of includes/etc.
                    >
                    > Is it "better" to replicate my form in HTML and insert <?PHP echo
                    > $variable; ?> in each tag?
                    > Or would it be better to somehow put all my PHP up front and echo the
                    > HTML at one time?
                    >
                    > BTW, I have this same debate on a couple of pages where I do mySQL
                    > queries 2-3 times on the same page... Advice, suggestions and
                    > recommendations are greatly appreciated!
                    >
                    > Thanx,
                    > Wm
                    >[/color]
                    I don't mix php and html. Whenn generating an html form, I load a
                    single variable with all the html code. Then print $var. All the
                    database stuff is done with functions.

                    --
                    11:15am up 12 days, 10:31, 1 user, load average: 1.00, 1.01, 1.04
                    103 processes: 101 sleeping, 2 running, 0 zombie, 0 stopped
                    CPU states: 2.0% user, 0.7% system, 1.0% nice, 0.2% idle
                    To email me, change .com to .ca Linux Counter Registration #126647

                    Comment

                    Working...