{"id":2358,"date":"2021-07-30T07:53:17","date_gmt":"2021-07-30T07:53:17","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=2358"},"modified":"2025-04-05T14:27:24","modified_gmt":"2025-04-05T14:27:24","slug":"php-heredoc","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-heredoc\/","title":{"rendered":"PHP Heredoc"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use PHP heredoc and nowdoc strings to improve the readability of the code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-heredoc-string'>Introduction to the PHP heredoc string <a href=\"#introduction-to-the-php-heredoc-string\" class=\"anchor\" id=\"introduction-to-the-php-heredoc-string\" title=\"Anchor for Introduction to the PHP heredoc string\">#<\/a><\/h2>\n\n\n\n<p>When you place <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-variables\/\">variables<\/a> in a <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-string\/\">double-quoted string<\/a>, PHP will expand the variable names. If a string contains the double quotes (&#8220;), you need to escape them using the backslash character(<code>\\<\/code>). For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$he = <span class=\"hljs-string\">'Bob'<\/span>;\n$she = <span class=\"hljs-string\">'Alice'<\/span>;\n\n$text = <span class=\"hljs-string\">\"$he said, \\\"PHP is awesome\\\".\n\\\"Of course.\\\" $she agreed.\"<\/span>;\n\n<span class=\"hljs-keyword\">echo<\/span> $text;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRoZSA9ICdCb2InOwokc2hlID0gJ0FsaWNlJzsKCiR0ZXh0ID0gIiRoZSBzYWlkLCBcIlBIUCBpcyBhd2Vzb21lXCIuClwiT2YgY291cnNlLlwiICRzaGUgYWdyZWVkLiI7CgplY2hvICR0ZXh0Ow\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">Bob said, <span class=\"hljs-string\">\"PHP is awesome\"<\/span>.\n<span class=\"hljs-string\">\"Of course.\"<\/span> Alice agreed.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>PHP heredoc strings behave like double-quoted strings without the double quotes. It means that they don&#8217;t need to escape quotes and expand variables. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$he = <span class=\"hljs-string\">'Bob'<\/span>;\n$she = <span class=\"hljs-string\">'Alice'<\/span>;\n\n$text = <span class=\"hljs-string\">&lt;&lt;&lt;TEXT\n<span class=\"hljs-subst\">$he<\/span> said \"PHP is awesome\".\n\"Of course\" <span class=\"hljs-subst\">$she<\/span> agreed.\"\nTEXT;<\/span>\n\n<span class=\"hljs-keyword\">echo<\/span> $text;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRoZSA9ICdCb2InOwokc2hlID0gJ0FsaWNlJzsKCiR0ZXh0ID0gPDw8VEVYVAokaGUgc2FpZCAiUEhQIGlzIGF3ZXNvbWUiLgoiT2YgY291cnNlIiAkc2hlIGFncmVlZC4iClRFWFQ7CgplY2hvICR0ZXh0Ow\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-heredoc-syntax'>PHP heredoc syntax <a href=\"#php-heredoc-syntax\" class=\"anchor\" id=\"php-heredoc-syntax\" title=\"Anchor for PHP heredoc syntax\">#<\/a><\/h2>\n\n\n\n<p>The following shows the syntax of a heredoc string:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$str = <span class=\"hljs-string\">&lt;&lt;&lt;IDENTIFIER\nplace a string here\nit can span multiple lines\nand include single quote ' and double quotes \"\nIDENTIFIER;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, start with the <code>&lt;&lt;&lt;<\/code> operator, an identifier, and a new line:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-string\">&lt;&lt;&lt;IDENTIFIER<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, specify the string, which can span multiple lines and include single quotes (&#8216;) or double quotes (&#8220;).<\/p>\n\n\n\n<p>Third, close the string with the same identifier.<\/p>\n\n\n\n<p>The identifier must contain only alphanumeric characters and underscores and start with an underscore or a non-digit character.<\/p>\n\n\n\n<p>The closing identifier must follow these rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Begins at the first column of the line<\/li>\n\n\n\n<li>Contains no other characters except a semicolon (<code>;<\/code>).<\/li>\n\n\n\n<li>The character before and after the closing identifier must be a newline character defined by the local operating system.<\/li>\n<\/ul>\n\n\n\n<p>The following shows an invalid heredoc string because the character before it is not a newline character:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$str = <span class=\"hljs-string\">&lt;&lt;&lt;IDENTIFIER\ninvalid<\/span>\n    IDENTIFIER;\n\n<span class=\"hljs-keyword\">echo<\/span> $str;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRzdHIgPSA8PDxJREVOVElGSUVSCmludmFsaWQKICAgIElERU5USUZJRVI7CgplY2hvICRzdHI7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>However, the following heredoc string is valid:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$str = <span class=\"hljs-string\">&lt;&lt;&lt;IDENTIFIER\n    valid\n    IDENTIFIER;\n\necho <span class=\"hljs-subst\">$str<\/span>;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRzdHIgPSA8PDxJREVOVElGSUVSCiAgICB2YWxpZAogICAgSURFTlRJRklFUjsKCmVjaG8gJHN0cjs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-heredoc-strings-use-cases'>PHP heredoc strings&#8217; use cases <a href=\"#php-heredoc-strings-use-cases\" class=\"anchor\" id=\"php-heredoc-strings-use-cases\" title=\"Anchor for PHP heredoc strings&#039; use cases\">#<\/a><\/h2>\n\n\n\n<p>In practice, you use the heredoc syntax to define a string that contains a single quote, double quotes, or variables. The heredoc string makes the string easier to read.<\/p>\n\n\n\n<p>Also, you can use a heredoc string to generate HTML dynamically. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$title = <span class=\"hljs-string\">'My site'<\/span>;\n\n$header = <span class=\"hljs-string\">&lt;&lt;&lt;HEADER\n&lt;header&gt;\n    &lt;h1&gt;<span class=\"hljs-subst\">$title<\/span>&lt;\/h1&gt;\n&lt;\/header&gt;\nHEADER;<\/span>\n\n<span class=\"hljs-keyword\">echo<\/span> $header;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiR0aXRsZSA9ICdNeSBzaXRlJzsKCiRoZWFkZXIgPSA8PDxIRUFERVIKPGhlYWRlcj4KICAgIDxoMT4kdGl0bGU8L2gxPgo8L2hlYWRlcj4KSEVBREVSOwoKZWNobyAkaGVhZGVyOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-nowdoc-syntax'>PHP Nowdoc syntax <a href=\"#php-nowdoc-syntax\" class=\"anchor\" id=\"php-nowdoc-syntax\" title=\"Anchor for PHP Nowdoc syntax\">#<\/a><\/h2>\n\n\n\n<p>A nowdoc string is similar to a heredoc string except that it doesn&#8217;t expand the variables. Here&#8217;s the syntax of a nowdoc string:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$str = <span class=\"hljs-string\">&lt;&lt;&lt;'IDENTIFIER'\nplace a string here\nit can span multiple lines\nand include single quote ' and double quotes \"\nIDENTIFIER;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The nowdoc&#8217;s syntax is similar to the heredoc&#8217;s syntax, except that the identifier that follows the &lt;&lt;&lt; operator needs to be enclosed in single quotes. The nowdoc&#8217;s identifier also follows the rules for the heredoc identifier.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Heredoc strings are like double-quoted strings without escaping.<\/li>\n\n\n\n<li>Nowdoc strings are like single-quoted strings without escaping.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2358\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-heredoc\/\"\n\t\t\t\tdata-post-title=\"PHP Heredoc\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2358\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-heredoc\/\"\n\t\t\t\tdata-post-title=\"PHP Heredoc\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use PHP heredoc and nowdoc strings to improve the readability of the code.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":13,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2358","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=2358"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2358\/revisions"}],"predecessor-version":[{"id":2963,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2358\/revisions\/2963"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=2358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}