#1
  1. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,042
    Rep Power
    535

    Echo verse open/close PHP tags


    In a view, is one approach better than the other? All the time, or just in certain cases? Why?

    PHP Code:
    <p>Some HTML</p>
    <?php if($flag){echo('Some more HTML');}?>
    <p>Even more HTML</p>
    PHP Code:
    <p>Some HTML</p>
    <?php if($flag){?>Some more HTML<?php}?>
    <p>Even more HTML</p>
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Normally that decision is based on one of three things:

    1) The length/content of the string. Many programmers find it easier to echo short one-line strings and close PHP for large blocks. Similarly, closing PHP makes it very easy to print strings containing both kinds of quotes.

    2) Personal preference. Similar to #1, some programmers are OCD enough to always close PHP when printing strings containing HTML.

    3) Coding standards. In some organizations, how you print strings is decided by someone else.

    Personally, I almost NEVER close PHP. I just never got into the habit and it's only when dealing with a file I consider a "template" or a whole-page script that I even consider it.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    I'd say both are bad. The first leads to a weird mixture of HTML and PHP code containing strings containing HTML. The latter will lead to an unreadable mess as soon as the contant spans multiple lines. Pick any Wordpress template, and you'll see what I mean.

    PHP simply isn't a template engine, at least not a good one. It can be (ab)used for this purpose, but the <?php ... ?> syntax is just horrible, and it lacks all advanced features like template inheritance, blocks etc.

    Consider using an actual template engine like Twig or Smart.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,042
    Rep Power
    535
    Thanks ManiacDan,

    4) The string is comprised of multiple php variables where I always use echo.

    I almost always use echo, however, my question related to mostly HTML templates.

    Maybe heredoc and nowdoc syntax are more appropriate?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,042
    Rep Power
    535
    Thanks Jacques1, I considered using a template engine, but never got around to trying. Do you have a preference?
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by NotionCommotion
    Do you have a preference?
    Twig. It has a nice syntax and all the fancy features.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Originally Posted by NotionCommotion
    Do you have a preference?
    PHP. PHP was written specifically to be an HTML templating language. All something like Twig does is enforce a coding standard by making PHP function calls impossible. If you maintain discipline in separating display logic from functional/business logic, there's no reason to use a templating engine. My company's experience with switching to twig was nothing but frustration and confusion, trying to figure out how to do something in Twig which would be trivial in PHP.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo