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!