PHP NOWDOC and HEREDOC


NOWDOC vs HEREDOC With PHP

With PHP, you can use NOWDOC and HEREDOC strings as opposed to traditional single quoted or double quoted strings. Using HEREDOC and NOWDOC strings can make life easier; especially when you have long strings with many single quotes, double quotes and variables.

NOWDOC

The NOWDOC string below starts with <<<'SQL' and end with SQL; followed by no whitespace. The name SQL was chosen since this is an SQL string. But, any other matching name like EOF could have been used in its place to yield the same results.

For large mySQL dumps such as resetting a mySQL database, using NOWDOC is an easy method to write a very long or complex query to use with a function like mysqli_query(). An example of this with our code would be:
$result = mysqli_query($db, $my_nowdoc)
.

<?php
$variable1b= 'test1'; 
$variable2 = 'test2'; 
$my_nowdoc = <<<'SQL'
INSERT INTO `tablename` (`blogger_id`, `blog_id`, `cat_id`, `title`, `notes`, `date_posted`, `date_deleted`) VALUES
(1, 286, 4, 'Codes-PHP-', '<p>$variable1b;</p>\r\n<p>$variable2 = &#39;all&#39;;</p>\r\n<p>Here is a new line</p>\r\n<p>Eventually it must cut off.</p>\r\n<p>Cut off.</p>\r\n', '2014-09-18 22:52:45', '0000-00-00 00:00:00'),
(1, 282, 2, 'Images-Width-100-Percent', '<p><a href="http://yahoo.ca">test</a>what</p>\r\n<p style="width:100%"><img src="images/promo.jpg" style="width:100%"></p>\r\n', '2014-08-27 23:37:39', '0000-00-00 00:00:00'),
(1, 303, 2, 'New-Steely-Dan-Post', '<p><img alt=\\"\\" height=\\"auto\\" src=\\"images/how-to.jpg\\" width=\\"100%\\"></p>\r\n', '2014-10-28 12:01:39', '0000-00-00 00:00:00')
SQL;

echo $my_nowdoc;

HEREDOC

The HEREDOC string below starts with <<<SQL and end with SQL; followed by no whitespace. The difference between this example and the one above is that there are no single quotes on the first line since it is those quotes that separate the NOWDOC from HEREDOC. Although the name SQL was chosen, any other matching name like EOF could have been used in its place to yield the same results.

<?php
$variable1b= 'test1'; 
$variable2 = 'test2'; 
//$my_nowdoc = <<<'SQL'
$my_heredoc = <<<SQL
INSERT INTO `raspberry_blogs` (`my_blogger_id`, `blog_id`, `cat_id`, `title`, `notes`, `date_posted`, `date_deleted`) VALUES
(1, 286, 4, 'Codes-PHP-', '<p>$variable1b;</p>\r\n<p>$variable2 = &#39;all&#39;;</p>\r\n<p>Here is a new line</p>\r\n<p>Eventually it must cut off.</p>\r\n<p>Cut off.</p>\r\n', '2014-09-18 22:52:45', '0000-00-00 00:00:00'),
(1, 282, 2, 'Images-Width-100-Percent', '<p><a href="http://yahoo.ca">test</a>what</p>\r\n<p style="width:100%"><img src="images/promo.jpg" style="width:100%"></p>\r\n', '2014-08-27 23:37:39', '0000-00-00 00:00:00'),
(1, 303, 2, 'New-Steely-Dan-Post', '<p><img alt=\\"\\" height=\\"auto\\" src=\\"images/how-to.jpg\\" width=\\"100%\\"></p>\r\n', '2014-10-28 12:01:39', '0000-00-00 00:00:00')
SQL;

echo $my_heredoc;

HTML HEREDOC

The example below shows how to make an HTML page into a HEREDOC string and print it.

<?php
$my_html_heredoc = <<<HTML
			<!DOCTYPE HTML>    
<html>
    <head>
        <title>My Page</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <meta name="description" content=""/>
        <meta name="keywords" content=""/>
        <link rel="stylesheet" href="css/style.css"/>        
    </head>

<body class="right-sidebar">

    <!-- Header Wrapper -->
    <div id="header-wrapper">

        <!-- Header -->
        <div id="header" class="container">

            <!-- Logo -->
            <h1 id="logo"><a href="#">MyTitle</a></h1>
                <!-- Nav -->
    <nav id="nav">
        <ul>
            
            <li>
                <a href="index.php">Blog</a>

            </li>
            <li>
                <a href="about.php">About</a>
            </li>
            <li class="break">
                <a href="help.php">Demo</a>
            </li>
            
            <li><a href="login.php">Login</a></li>
        </ul>
    </nav>

        </div>

    </div>

    <!-- Main Wrapper -->
<div class="wrapper">

    <div class="container">

        <div class="row" id="main">

            <div class="8u">

                <div><h3 class="help-h3"MySubheading</h3></div>

                <!-- BEGIN SEGMENT-->
                <div>
                    <div id="setup"><strong>How It Began</strong></div>
                    <div>Any text can go here.
                    </div>
                </div>
                <!-- END SEGMENT-->            

            </div>
           
        <div class="my-clear"></div>
    </div>


<!-- BEGIN ALL PAGES ON WEBSITE -->
<div class="row features">  
        
        <header>
            <h3>Use On Any Device</h3>
        </header>
        <p>Line #1</p>
        <ul class="actions">
            <li><a href="#" class="button">Next Line</a></li>
        </ul>  

</div>

</div></div>

</body>
</html>
HTML;

echo $my_html_heredoc;