can't get multiline to work with heredoc

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MacForMore
Forum Newbie
Posts: 2
Joined: Sun Oct 04, 2009 7:46 am

can't get multiline to work with heredoc

Post by MacForMore »

I'm working some samples, learning PHP, and I can't get the heredoc construct to output multiline text in the browser.

My samples come from an O'Reilly book and various websites.

Am I doing something wrong?

I'm a mac newbie doing this in Netbeans on the mac with MAMP 1.8.2 installed. Mamp has PHP 5 with Zend Extensions (don't know what that is). I copied the text for index.php to another editor, thinking it might be something with netbeans, but that didn't help.

this code, below, outputs only one line.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        $variables = "a test variable";
$string = <<< END_OF_STRING
            this is a multi-line
            string containing variables
            and single quotes '
            and double quotes "
            and slashes \
            do not need to be escaped
END_OF_STRING;
        echo $string
        ?>
    </body>
</html>
 
 
the output in the browser looks like this
this is a multi-line string containing a test variable and single quotes ' and double quotes " and slashes \ do not need to be escaped
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: can't get multiline to work with heredoc

Post by Darhazer »

It outputs the whole string, which means it works.
Maybe you are expecting to see it as multilne text in the browser? Go learn something about HTML and new lines...

Code: Select all

echo nl2br( $string );
MacForMore
Forum Newbie
Posts: 2
Joined: Sun Oct 04, 2009 7:46 am

Re: can't get multiline to work with heredoc

Post by MacForMore »

Darhazer wrote:It outputs the whole string, which means it works.
Maybe you are expecting to see it as multilne text in the browser? Go learn something about HTML and new lines...

Code: Select all

echo nl2br( $string );
Thanks for the tip about nl2br. All the examples I saw presented the heredoc as though it would automatically include the breaks when echoed.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: can't get multiline to work with heredoc

Post by requinix »

MacForMore wrote:Thanks for the tip about nl2br. All the examples I saw presented the heredoc as though it would automatically include the breaks when echoed.
What you're supposed to go learn is that in HTML multiple whitespace (spaces, tabs, newlines...) all get collapsed into one space.

If you want to see the raw output do a View Source of the page.
Post Reply