Women in Technology

Hear us Roar

  PHP and Heredocs
Subject:   Why not use php-style instead of perlish ?
Date:   2003-04-12 06:42:57
From:   anonymous2
Most of your examples can be written using simple php-style for strings. This isn't possible in PERL, but in PHP thats easy.

$var="SELECT *
FROM $table
WHERE a='1' ";

is better readable in my opinion and don't introduce the <<< Statement which can be confusing to newbies.

Also you started with separation of code and presentation, but mix them in one file, its better to have seperate files, and next would be to use a simple Template system, there are lots available for PHP. Because only then you are save that the designer does not 'break' your code.


Full Threads Oldest First

Showing messages 1 through 5 of 5.

  • Why not use php-style instead of perlish ?
    2003-04-14 12:05:23  chromatic | O'Reilly AuthorO'Reilly Blogger [View]

    Sorry, that's incorrect.

    Perl handles multi-line variable assignments. I just tried it on Perl 1 -- from 1987 -- and it worked just fine.
  • Why not use php-style instead of perlish (2)?
    2003-04-12 06:55:56  anonymous2 [View]

    Sorry was not detailed enough.

    The example should look like this:
    SELECT *
    FROM $table
    WHERE field='1' ";

    You can have singlequotes in it with no problem.
    If you really need Doublequotes you can first try if you can use 'SELECT ... field="2" '; but then Variables are not evaluated. But in many cases you dont need the heredoc, which is essential for PERL, because there you can't use the multiline string like in PHP.

    Actually you use a kind of Templatesystem, but it seems a little complicated. I meen you should n't have ANY php in the template but only marker. You can read in the file simply with:
    $template_str=join("", file("...path/template.php"); Which also does the trick. And the Designer sees NO php at all.
    In your template you can use some marker like {$var} for displaying content which is not php either.

    On the other hand, heredoc are useful if you have strings with many quotes and so on.

    • Why not use php-style instead of perlish (2)?
      2003-04-12 11:34:46  Daniel Smith | [View]

      Hi, I am Daniel Smith, the author:

      I use heredocs SPECIFICALLY so that I do not have to write things such as:

      SELECT *
      FROM $table
      WHERE field='1' ";

      Which introduces a new level of quoting.

      As far as reading complete templates into a variable, so that the template is free of PHP assignment statements.. Yes, that is true. It's a little more work, in a sense, if there is a middle section of the template that gets repeated.. it has to be broken out (via HTML comments or otherwise) so that it gets assigned to a different variable (or element in an associative array)

      In other words, if you have a template of head, middle, and tail sections, you can break them up into seperate heredocs, or you can read a straight HTML file in and muddle through it, breaking up the sections afterward. Think of the middle section as any variable number of results (a table, a collection of photos, etc.)

      You may want to take a look at my example demo file.
      It is at: http://www.onlamp.com/php/2003/04/10/examples/hd-demo-1.0.tgz

      as an aside: I did not get into caching in my article, but it is something I am working on for my project. I don't want to simply read in templates for every invocation if I don't need to.

      The reason I approached things this way in the first place was because I wanted to keep my package as self-contained as possible (fewer dependencies for future admins to deal with), but I also wanted to handle different languages and interfaces.

      Daniel Smith
      • Why not use php-style instead of perlish (2)?
        2003-04-15 03:32:34  anonymous2 [View]

        HEREDOC strings are very convenient to spit out HTML, or any lengthy string blocks and it's not Perlish (!) it's derived from UNIX shell's here-document syntax!! I use both Perl and PHP for my web projects and it is pointless to say that PHP is better than Perl (both languages are fun to program with, and PHP really needs to level up with Perl regarding OOP).

        Please drop by "http://theperlreview.com/" and read Ed Summer's article "Paying Homage to Perl (PHP) " in V.0-I.7
        • Why not use php-style instead of perlish (2)?
          2003-04-15 09:48:25  Daniel Smith | [View]

          Yes, I completely agree. I think you meant to reply to a different message in the thread, but I know what you mean.

          My first exposure to heredocs was in the early 1980's, writing csh scripts. It's one thing missing in C/C++ that I really would have enjoyed having from time to time. Come to think of it, it would have been nice in Java, Javascript, and Tcl too.

          A few languages supporting heredocs:

          One of many things I like about heredocs is that using them is much less cluttered than a series of print/echo statements. It allows me to better concentrate on the content within the heredoc, such as an email message, a help blurb, or anything else that tends to be multiline.

          > Please drop by "http://theperlreview.com/" and read Ed Summer's article "Paying Homage to Perl (PHP) " in V.0-I.7

          Interestingly, that PDF file contains an article "Separating Code, Presentation, and Configuration -- brian d foy" which makes use of heredocs in Perl :-)

          Daniel Smith