better programming

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

    better programming

    Hi,

    A quick and most likely simply question about which is generally better
    programming with PHP. Is it more proper to break out of PHP code to write
    HTML, or is it ok rto do it within print() statements? Such as...

    SCRENARIO A)
    <?php
    ....code...
    ?>
    <img src="<?=$imgsrc ?>">
    <?php
    ....more code...
    ?>

    SCENARIO B)
    <?php
    ....code...
    print("<img src=\"".$imgsrc ."\">");
    ....more code...
    ?>

    Thanks,
    Michael


  • Rik

    #2
    Re: better programming

    Michael wrote:
    Hi,
    >
    A quick and most likely simply question about which is generally
    better programming with PHP. Is it more proper to break out of PHP
    code to write HTML, or is it ok rto do it within print() statements?
    Such as...
    >
    SCRENARIO A)
    <?php
    ...code...
    >>
    <img src="<?=$imgsrc ?>">
    <?php
    ...more code...
    >>
    >
    SCENARIO B)
    <?php
    ...code...
    print("<img src=\"".$imgsrc ."\">");
    ...more code...
    It highly depends, and there is no easy answer. I usually break out of php
    for large blocks, and stay inside for small bits.

    However, the reason I post;
    1. <?=$imgsrc?is a Bad Thing
    It can work, no question about that, but always try to use a proper
    <?php echo ''; ?>.

    2. print("<img src=\"".$imgsrc ."\">");
    Better would be either print("<img src=\"{$imgsrc} \">"); or print('<img
    src="'.$imgsrc. '">');

    Grtz,
    --
    Rik Wasmus


    Comment

    • Peter Fox

      #3
      Re: better programming

      Following on from Michael's message. . .
      >Hi,
      >
      >A quick and most likely simply question about which is generally better
      >programming with PHP. Is it more proper to break out of PHP code to write
      >HTML, or is it ok rto do it within print() statements? Such as...
      Of course there is no 'best', 'best practice' or 'proper' [Hey will
      posters /please/ stop asking for 'best practice'!]

      Personally I _write in PHP_ and so print() everything. This gives you
      the flexibility to stop hard-coding any time you please.

      For example :
      $address = "57 High St., Huddersfield"; // could be define()
      print("You can visit us at $address Monday to Friday 9 - 5");

      $address can be derived from an include/configuration file. This means
      there is one more thing to go wrong at initial setup but it also means
      that it can cope with moving premises AND setting up a second location
      in a single place.

      It also means that if you think 'objects' you can emit objectively (as
      it were) rather than HTML-ing every instance.

      This gets more serious when you want to mix and mingle with CSS. An
      'Address' object might be used for printing postal address to a screen,
      a postcode to a map URL or a company address for an invoice (using DX
      for solicitors etc.).

      And if you're ever thinking of internationalis ation ...

      --
      PETER FOX Not the same since the statuette business went bust
      peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
      2 Tees Close, Witham, Essex.
      Gravity beer in Essex <http://www.eminent.dem on.co.uk>

      Comment

      • Rik

        #4
        Re: better programming

        Peter Fox wrote:
        For example :
        $address = "57 High St., Huddersfield"; // could be define()
        print("You can visit us at $address Monday to Friday 9 - 5");
        >
        >
        And if you're ever thinking of internationalis ation ...
        Good point:

        $format['visitus'] = array(
        'en' ='You can visit us at %s Monday to Friday 9 - 5',
        'nl' ='U kunt ons van maandag tot vrijdag tussen 9 - 5 op %s
        bezoeken.');

        $lang = 'en'; //or somthing else

        printf($format['visitus'][$lang],$adress);

        Makes it a lot simpler to change languages then the other option.
        --
        Rik Wasmus


        Comment

        • Martin Mandl - m2m tech support

          #5
          Re: better programming

          Why don't you try to separate your code from the html part completely,
          e.g. with Smarty (http://smarty.php.net) ???

          Michael wrote:
          Hi,
          >
          A quick and most likely simply question about which is generally better
          programming with PHP. Is it more proper to break out of PHP code to write
          HTML, or is it ok rto do it within print() statements? Such as...
          >
          SCRENARIO A)
          <?php
          ...code...
          ?>
          <img src="<?=$imgsrc ?>">
          <?php
          ...more code...
          ?>
          >
          SCENARIO B)
          <?php
          ...code...
          print("<img src=\"".$imgsrc ."\">");
          ...more code...
          ?>
          >
          Thanks,
          Michael

          Comment

          • Stefan Rybacki

            #6
            Re: better programming

            -----BEGIN PGP SIGNED MESSAGE-----
            Hash: SHA1

            Martin Mandl - m2m tech support schrieb:
            Why don't you try to separate your code from the html part completely,
            e.g. with Smarty (http://smarty.php.net) ???
            Good call, thought nobody would suggest this IMHO best solution for the
            proposed question, where I don't mean that Smarty is the best solution,
            but the idea of separating business logic from representation.

            Thanks
            Stefan
            >
            >...
            -----BEGIN PGP SIGNATURE-----
            Version: GnuPG v1.4.2.1 (MingW32)

            iD8DBQFFnkUwyeC Lzp/JKjARArNIAJ9LIP x7YE+rvVlbVjgt3 5trTunZqACaApNB
            lRTJEr/mE6yaMAJonc1Yf9 c=
            =PicZ
            -----END PGP SIGNATURE-----

            Comment

            • Kenneth Downs

              #7
              Re: better programming

              Michael wrote:
              Hi,
              >
              A quick and most likely simply question about which is generally better
              programming with PHP. Is it more proper to break out of PHP code to write
              HTML, or is it ok rto do it within print() statements? Such as...
              I find code easier to read if it is almost entirely PHP or almost entirely
              HTML. Therefore, large blocks of static HTML with little or no dynamic
              content will use html:

              // php code goes here....
              ?>
              <h1>Welcome to the Wizard!</h1>
              <p>Welcome, <?=$user_id?> , to our wizard, we hope
              that you will find it easy to use, and all
              of that nonsense.
              <?php
              // ...more php code

              However, if I am stringing together text that is more variables than static,
              it tends to look better and be easier to read if it is a list of PHP
              commands:

              $row=SQL_OneRow ("Select * from customers...")
              echo "Hello ".$row['user_id'];
              echo ", I see you are from ".$row['city'];
              echo ", and that you have ".$row['eyecolor']." eyes";

              >
              SCRENARIO A)
              <?php
              ...code...
              ?>
              <img src="<?=$imgsrc ?>">
              <?php
              ...more code...
              ?>
              >
              SCENARIO B)
              <?php
              ...code...
              print("<img src=\"".$imgsrc ."\">");
              ...more code...
              ?>
              >
              Thanks,
              Michael
              --
              Kenneth Downs
              Secure Data Software, Inc.
              (Ken)nneth@(Sec )ure(Dat)a(.com )

              Comment

              Working...