FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Heredoc print to file? Use nowdoc.
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Heredoc print to file? Use nowdoc. [message #172118 is a reply to message #172114] Mon, 31 January 2011 04:44 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
Senior Member
On 1/30/2011 9:38 PM, P E Schoen wrote:
> I ran into an issue with a heredoc that works in Perl, but apparently
> not in PHP. I want to write the output of the heredoc to a file other
> than stdin, which is done in Perl as:
>
> print fOutput <<EOF ;
> My Text
> My HTML
> EOF
>
> and you can use
>
> print <<EOF ;
> ...
> EOF
>
> for output (echo) to stdin.
>
> But in PHP it is either:
>
> print <<<EOF
> ...
> EOF;
>
> or
>
> echo <<<EOF
> ...
> EOF;
>
> And there seems to be no way to do this. There is also a bug
> report/request on the subtle behavior of the heredoc regarding whitespace:
> http://bugs.php.net/bug.php?id=24750
>
> But I found something called a nowdoc which might allow this:
>
> $str = <<<'EOD'
> Example of string
> spanning multiple lines
> using nowdoc syntax.
> EOD;
> fwrite( $fh, $str );
>
> http://www.php.net/manual/en/language.types.string.php#language.types.strin g.syntax.heredoc
>
>
> OK, that works. I have changed the subject to reflect it has been
> solved. This is still a good reference for other newbies and Perlites.
>
> I also tried using $stdout but it did not work, although the following
> seems to hint that it might:
> http://php.net/manual/en/features.commandline.io-streams.php
>
> Paul

First of all, don't try to compare PHP an Perl. They are two different
languages, and what you can do in one may or may not be done in the
other (and it goes both ways).

No, you can't write to a file that way in PHP. But you can use
fopen()/fwrite()/fclose() for strings built with heredoc or any other
string. It's not restricted to nowdoc.

And you can use $stdout - but there is nothing special about this
variable. You need to open the stream as shown in the example:

$stdout = fopen('php://stdout', 'w');

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: Unable to connect to database server!
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 01:39:52 GMT 2024

Total time taken to generate the page: 0.05149 seconds