Page 1 of 1

Trouble using heredoc to store a php file as a string

Posted: Thu Oct 13, 2011 6:31 pm
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

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

Posted: Fri Oct 14, 2011 10:10 am
by ouchiko
<&#63;php echo "Welcome to my PHP page"; &#63;>

??

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

Posted: Thu Oct 20, 2011 12:59 pm
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!

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

Posted: Thu Oct 20, 2011 1:27 pm
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

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

Posted: Thu Oct 20, 2011 2:15 pm
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