0

I have run across a special version of PHP syntax that I never knew before several times now and it is starting to grow on me. Most of our framework uses echo to build pages, rather than continually opening and closing with PHP tags (<?php and ?>); However, this gets extremely frustrating when outputting small scripts and HTML attributes, as we need to be careful about how we escape things, leading to segments like this:

echo ("<form action='$formurl' id='testForm$num' class='hor-form'>" . 
    "    <script>" .
    "        $('#txtInput').clear();" .
    "        if (true) {" .
    "            $('#errorMessage').html('<input type=\"text\" id=\"test\">');" .
    "        }" .
    "    </script>" .
    "    <input id='txtInput' type='text' />" . 
    "    <input id='btnSubmit' type='submit' value='Submit' />" . 
    "</form>"
);

Obviously this is absolutely fake, but hopefully gets the point across. I'm not advocating how it is done, but it is what it is. Recently though, I've started to see it done this way:

echo<<<HTML
    <form action="$formurl" id="textForm$num" class="hor-form">
        <script>
            $("#txtInput").clear();
            if (true) {
                $("#errorMessage").html("<input type='text' id='text'>");
            }
        </script>
        <input id="txtInput" type="text">
        <input id="btnSubmit" type="submit" value="Submit">
    </form>
HTML;

The syntax appears to be as follows:

echo<<<RANDOM_NAME
    //HTML code, possibly other syntaxes as well but likely not
RANDOM_NAME;

It is absolutely much nicer, as it actually gives proper syntax highlighting and escaping quotes/appending strings is no longer necessary! The only problem is that I can find absolutely no documentation for it anywhere!. It may be that I don't know what to search for, and indeed I don't, but I have only found anything involving it once and I can't locate it again.

I have a lot of questions about it (like can I use PHP functions from within it, or what types of variables can I use, etc) but nowhere to go for solutions. Also, can I do something other than directly using echo? For example, can I use a variable instead of echo? If so, how?

If someone could enlighten me that would be excellent!

Kendall
  • 1,992
  • 7
  • 28
  • 46
  • 2
    http://php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc and heredoc on the same page. – u_mulder Jun 06 '16 at 17:56
  • 3
    It's known as "heredoc syntax" http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc and you can use it in any context that accepts a string, but the ending delimiter `RANDOM_NAME` is fussy about its position. – Michael Berkowski Jun 06 '16 at 17:56
  • Why not to use open-close syntax. For example use ` html_here } else { ?> other_html_for_else /*some other php*/ ?>`. Did you get my point ot you want me to add an answer ? –  Jun 06 '16 at 17:59
  • @GeorgeGkas - why use open-close syntax when heredoc is some much easier and cleaner – Mark Baker Jun 06 '16 at 18:02
  • @GeorgeGkas If you'll notice I kind of alluded to not wanting to use open/close, but didn't explicity say so. I agree with Mark, I prefer what I now know as heredoc. – Kendall Jun 06 '16 at 18:05
  • @MichaelBerkowski thank you, I honestly did not come across that in my searches between work. My apologies for the duplicate – Kendall Jun 06 '16 at 18:06
  • @KendallRoth No problem. It's difficult to search for symbols, but many of us have a mental map of the landscape of PHP questions to link as signposts. – Michael Berkowski Jun 06 '16 at 18:09
  • @MarkBaker I use it for MVC architecture design. It works for me. Do you believe that I should stop using it? –  Jun 06 '16 at 18:10
  • The one thing I do not like about about heredoc (and nowdoc) is that the ending identifier must contain nothing else other than the identifier itself and a semicolon. So no indenting for example – georaldc Jun 06 '16 at 18:19
  • @GeorgeGkas - Use what works in the appropriate circumstance; but dropping in and out of PHP every line over several lines is often less readable and more costly in performance – Mark Baker Jun 06 '16 at 20:32

0 Answers0