payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

Please support if you like my work by payment through upi: sinhamit@icici or payment by bank

account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB


Heredoc and Nowdoc   in Category: PHP   by amit

🕙 Posted on 2023-05-12 at 06:34:06     Read in Hindi ...


Heredoc & Nowdoc

    Heredoc and Nowdoc are two newer ways to handle string data-type, as they are placed in a pair of double quotes or a pair of single quotes, respectively. ` (tick symbol, just left to 1 key in the keyboard) is used in pair, in ECMAScript 2015 or ES6 (in JavaScript) in the same way as Heredoc works in PHP. You can use any special characters inside Heredoc and Nowdoc without help of escape sequences. Backslash is also treated as literal in these two ways.

    Heredoc and Nowdoc, both are written with an unique identifier at the beginning and at the end of the string. The beginning identifier in both HEREDOC and NOWDOC, are prefixed with <<< (three less than symbol), written without any space between them and also, without any space with the identifier. You must use some random UNIQUE text, which will not be used as identifier within the string! If any text inside the string is found same as opening IDENTIFIER, then that text will close the string, and you will get ERROR. You must add ; (semi-colon symbol) after the ending or closing identifier.

HEREDOC treats string as enclosed in a pair of double quotes

    Beginning identifier either enclosed within a pair of double quotes, or without double quotes or without single quotes, make it HEREDOC. You have to make proper indentation when using HEREDOC & NOWDOC. The VSCode Editor and other modern code editors will show ERROR (underlined in red color squigly line), when there is no proper indentation, and it will also be shown in web-browser.


Parse error: Invalid body indentation level (expecting an indentation level of at least 2) in C:\xampp\htdocs\php_project\phptest.php on line 4

    Any space with spacebar key or Tab key, or line-break with Enter/Return key shall be counted in both HEREDOC and NOWDOC. Do not place comments inside HEREDOC or NOWDOC, because these will be treated as STRING data-type, and PHP will not process them as comment. nl2br() is a pre-defined (built-in) PHP function, (generally used,) to insert the <br> HTML element, where the line-feed is inserted either with \n or Enter/Return key of the keyboard.

<?php
  $one = 'abcd';   $two = 'efgh';   $three = 'ijkl';   $four = 'mnop';
  $text = <<<IDENTIFIER
  line $one
  line $two
  line $three
  line $four
  IDENTIFIER;
  echo nl2br( $text );
?><br />

line abcd
line efgh
line ijkl
line mnop

<?php
  $one = 'abcd';   $two = 'efgh';   $three = 'ijkl';   $four = 'mnop';
  $text = <<<"TEXTIDENTIFIER"
  line$one
  line$two
  line$three
  line$four
  TEXTIDENTIFIER;
  echo nl2br( $text );
?><br />

lineabcd
lineefgh
lineijkl
linemnop

Nowdoc treats string as enclosed in a pair of single quotes

    Beginning identifier enclosed in a pair of single quotes makes it NOWDOC. Variables are not expanded to their values, in following example:

<?php
  $one = 'abcd';   $two = 'efgh';   $three = 'ijkl';   $four = 'mnop';
  $text = <<<'NEWIDENTIFIER'
  line $one
  line $two
  line $three
  line $four
  NEWIDENTIFIER;
  echo nl2br( $text );
?><br />

line $one
line $two
line $three
line $four

Heredoc & Nowdoc are generally used to insert HTML structure

<?php
$html_structure = <<<UNIQUETEXT
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta
name="viewport" content="width=device-width, initial-scale=1" />
    <title>R.I.B.H.U. Academy</title>
    <style type="text/css">
      /* === EMBEDDED CSS STYLES === */
      .example { background-color: skyblue; font-size: 2rem; width: 50%; }
    </style>
  </head>
  <body>
    <div class="example">
      <p> WELCOME TO RIBHU ACADEMY </p>
    <div>

    <script type="text/javascript">
        // JavaScript codes ...
    </script>
  </body>
</html>

UNIQUETEXT;
?>

<?php echo $html_structure; ?>

<?php var_dump( $html_structure ); ?>

    Caution: Though var_dump() function can display the output in the web-browser, but you should avoid using it, when the string contains any HTML Doctype declaration.

string(616) "<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>R.I.B.H.U. Academy</title>
    <style type="text/css">
      /* === EMBEDDED CSS STYLES === */
      .example { background-color: skyblue; font-size: 2rem; width: 50%; }
    </style>
  </head>
  <body>
    <div class="example">
      <p> WELCOME TO RIBHU ACADEMY </p>
    <div>

    <script type="text/javascript">
      // JavaScript codes ...
    </script>
  </body>
</html>"

Leave a Comment: