{"id":3015,"date":"2020-07-06T18:47:50","date_gmt":"2020-07-06T17:47:50","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=3015"},"modified":"2025-11-30T16:29:56","modified_gmt":"2025-11-30T16:29:56","slug":"heredoc-string","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/strings\/heredoc-string","title":{"rendered":"Heredoc string"},"content":{"rendered":"\n<p>You can use the &lt;&lt;&lt; sequence like this (this is called a Heredoc string):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$HtmlOutput = &lt;&lt;&lt;_END\n  &lt;form method=\"GET\"&gt;\n    &lt;div&gt;Search Term: &lt;input type=\"search\" id=\"q\" name=\"q\"&gt;&lt;\/div&gt;\n    &lt;div&gt;Max Results: &lt;input type=\"number\" id=\"maxResults\" name=\"maxResults\" min=\"1\" max=\"50\" step=\"1\" value=\"25\"&gt;&lt;\/div&gt;\n    &lt;input type=\"submit\" value=\"Search\"&gt;\n  &lt;\/form&gt;\n_END;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>  $HtmlOutput = \"\";\n  $HtmlOutput .= &lt;&lt;&lt;_END\n\n_END;<\/code><\/pre>\n\n\n\n<p><strong><em>The terminating marker must be at the start of a line and the only thing on the line (no comment or even whitespace after it!!!)<\/em><\/strong><\/p>\n\n\n\n<p>The advantage of this is that there is no need for line breaks, escape characters (\\\u2019 etc) \u2013 what is between the markers is output exactly as it appears but with any variable names evaluated.&nbsp; _END can be anything.&nbsp; You can also use &amp;MyText &lt;&lt;&lt; _END \u2026 to assign the contents to a variable instead of echoing.<\/p>\n\n\n\n<p>You can include variables within the text. For complex variables you can enclose them in curley brackets like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>blah blah {$foo-&gt;bar&#91;1]} blah<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Using with echo<\/h5>\n\n\n\n<p>You can do this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  echo &lt;&lt;&lt;_END\n\n_END;<\/code><\/pre>\n\n\n\n<p>or this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $HtmlOutput = \"\";\n  $HtmlOutput .= &lt;&lt;&lt;_END\n\n_END;\n  echo($HtmlOutput);<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Including a function call<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>${!${''} =(time())}\n\n$HtmlOutput = &lt;&lt;&lt;_END\n  &lt;p&gt;Somthing ${!${''} =(time())} someting&lt;\/p&gt;\n_END;<\/code><\/pre>\n\n\n\n<p>(<a href=\"https:\/\/stackoverflow.com\/questions\/104516\/calling-php-functions-within-heredoc-strings\">This is based on the topic here<\/a>&nbsp;and relies on&nbsp;<a href=\"https:\/\/tpunt.github.io\/jekyll\/update\/2016\/01\/25\/php-parser-hack.html\">this nifty parser hack<\/a>)<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Using newline &#8220;\\n&#8221; etc in a Heredoc string<\/h4>\n\n\n\n<p>Any \\n inside double-quoted JS strings will be interpreted as real newlines, and sometimes break the heredoc or cause PHP to think you are escaping things.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Use NOWDOC instead of HEREDOC<\/h5>\n\n\n<p>No interpretation happens inside a NOWDOC:<\/p>\n<p style=\"padding-left: 40px;\">\\n is not turned into newlines<br \/>$variables are not expanded<br \/>Backslashes are not special<br \/>Perfect for embedding JavaScript\/HTML templates<\/p>\n\n\n<pre class=\"wp-block-code\"><code>  $HtmlOutput .= &lt;&lt;&lt;'_END'\n\n_END;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">If you must use HEREDOC (&lt;&lt;&lt;HTML) instead of NOWDOC<\/h5>\n\n\n\n<p>You must escape backslashes so \\n becomes \\n.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $HtmlOutput .= &lt;&lt;&lt;_END\n    &lt;script>\n      console.log(\"This will not break: \\\\n \\\\t\");\n    &lt;\/script>\n_END;<\/code><\/pre>\n\n\n\n<p>This can get messy fast though :-)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can use the &lt;&lt;&lt; sequence like this (this is called a Heredoc string): The terminating marker must be at the start of a line and the only thing on the line (no comment or even whitespace after it!!!) The advantage of this is that there is no need for line breaks, escape characters (\\\u2019 [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-3015","post","type-post","status-publish","format-standard","hentry","category-strings"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=3015"}],"version-history":[{"count":9,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3015\/revisions"}],"predecessor-version":[{"id":5191,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3015\/revisions\/5191"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=3015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=3015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=3015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}