Trouble using heredoc to store a php file as a string

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
tenleftfingers
Forum Newbie
Posts: 9
Joined: Thu Oct 13, 2011 6:19 pm
Location: Ireland

Trouble using heredoc to store a php file as a string

Post by tenleftfingers »

I got the following example of heredoc from: http://tuxradar.com/practicalphp/2/6/3

Code: Select all

$mystring = <<<EOT
    This is some PHP text.
    It is completely free
    I can use "double quotes"
    and 'single quotes',
    plus $variables too, which will
    be properly converted to their values,
    you can even type EOT, as long as it
    is not alone on a line, like this:
EOT;
It works fine, but I want to use heredoc to store a string starting with <?php so I can write it to a file as a .php page.

This code outputs the same as the last example above in spite of my addition:

Code: Select all

$mystring = <<<EOT
    <?php echo "Welcome to my PHP page"; ?>
    It is completely free
    I can use "double quotes"
    and 'single quotes',
    plus $variables too, which will
    be properly converted to their values,
    you can even type EOT, as long as it
    is not alone on a line, like this:
EOT;
How can I keep the <?php ...?> content as well so that I can use it to output a php page? I've done quite a bit of searching but noone seems to be using it in this way and the examples don't address this usage scenario.

Many thanks!
Ten
ouchiko
Forum Commoner
Posts: 35
Joined: Sun Oct 09, 2011 6:54 pm
Location: London

Re: Trouble using heredoc to store a php file as a string

Post by ouchiko »

<&#63;php echo "Welcome to my PHP page"; &#63;>

??
tenleftfingers
Forum Newbie
Posts: 9
Joined: Thu Oct 13, 2011 6:19 pm
Location: Ireland

Re: Trouble using heredoc to store a php file as a string

Post by tenleftfingers »

I wish it were that simple. I'm not sure what the limitations of heredoc are, but the description I read lead me to believe you could store *any* characters in it as a string. But this, for example:

Code: Select all

$mystring = <<<EOT
	<?php
	$val = 1;
	$val++;
    echo "<h1>header text</h1><p>Some more info deserving of a paragraph.</p><p>Another paragraph</p>.";
	echo $val;
	?>
EOT;
file_put_contents("myfile.php", $mystring);
Leaves me with a file that looks exactly like this:

Code: Select all

<?php
	 = 1;
	++;
    echo "<h1>header text</h1><p>Some more info deserving of a paragraph.</p><p>Another paragraph</p>.";
	echo ;
	?>
I'll try to find a better resource for this and update the thread. But I'm not certain if heredoc can do what I want.

Edit: escaping the variable with a \ fixed my problem. So anywhere you have $var you need to put \$var. Very happy to be able to spit out php pages easily!
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Trouble using heredoc to store a php file as a string

Post by twinedev »

If you are going to be using PHP 5.3.x or better, you can use nowdoc:

Code: Select all

$mystring = <<<'EOT'
        <?php
        $val = 1;
        $val++;
    echo "<h1>header text</h1><p>Some more info deserving of a paragraph.</p><p>Another paragraph</p>.";
        echo $val;
        ?>
EOT;
file_put_contents("myfile.php", $mystring);
Note the single quotes around EOT

See here for more information: http://us3.php.net/manual/en/language.t ... tax.nowdoc
tenleftfingers
Forum Newbie
Posts: 9
Joined: Thu Oct 13, 2011 6:19 pm
Location: Ireland

Re: Trouble using heredoc to store a php file as a string

Post by tenleftfingers »

Thanks twinedev. That's exactly what I'm looking for and the link clears up some confusion I was having.

Many thanks,
Ten
Post Reply