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
Switch to threaded view of this topic Create a new topic Submit Reply
Heredoc print to file? Use nowdoc. [message #172114] Mon, 31 January 2011 02:38 Go to next message
P E Schoen is currently offline  P E Schoen
Messages: 86
Registered: January 2011
Karma: 0
Member
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
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 next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
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
==================
Re: Heredoc print to file? Use nowdoc. [message #172123 is a reply to message #172118] Mon, 31 January 2011 07:31 Go to previous messageGo to next message
P E Schoen is currently offline  P E Schoen
Messages: 86
Registered: January 2011
Karma: 0
Member
"Jerry Stuckle" wrote in message
news:ii5eo4$ov7$1(at)news(dot)eternal-september(dot)org...

> 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).

Yes, I can see that, and it has been a learning experience doing the same
thing (which should be possible) in different ways (due to the language
differences). Now I'm having some difficulty with arrays, but I think I
found how to do what I want, although it seems ridiculous. I modified an
example in the manual to get this:

$qAll = "SELECT * FROM tEntries";
$result = $db->query($qAll);
$row = array("myArray");
$i = 0;
while($res = $result->fetcharray(SQLITE3_BOTH)){
if(!isset( $res['eid'] )) continue;
$row[$i][0] = $res[0];
$row[$i][1] = $res[1];
$row[$i]['et'] = $res['et'];
$i++;
}
print_r($row);
$maxrow = $i-1;
for($i=0; $i<$maxrow; $i++) {
fwrite( $fLog, "{$row[$i][0]} {$row[$i][1]} {$row[$i]['et']}\n");
fwrite( $fLog, "$row[$i][0] $row[$i][1] $row[$i]['et']\n");
}

I can't understand why you can assign an array element $row[$i][0] =
$res[0]; which is a simple scalar quantity (in this case an integer), but
when you use the same expression to write or print, it shows: Array[0], and
the curly braces are needed to extract to actual value. I only found this
deep in the user-supplied examples for arrays, and one would think this is
important enough to include in the main documentation.
http://www.php.net/manual/en/language.types.array.php

I didn't see anything in the array operators:
http://www.php.net/manual/en/language.operators.array.php

It’s not in operator precedence:
http://www.php.net/manual/en/language.operators.precedence.php

I did a search for php curly braces:
http://cowburn.info/2008/01/12/php-vars-curly-braces/

And from that I found this:
http://php.net/manual/en/language.types.string.php

So, now that I know this, I can proceed. It's probably still better than
Perl's various uses of $, %, #, and (), [], {}. Then you can throw in the @
operator and -> and => and all the other ASCII characters for a real
alphabet soup. OK, I'll stop complaining now. But I feel almost like the
victim of a joke, and I think I'll be fooled a few more times before I get
this project working.

:)

Paul
Re: Heredoc print to file? Use nowdoc. [message #172125 is a reply to message #172123] Mon, 31 January 2011 11:32 Go to previous messageGo to next message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma: 0
Senior Member
P E Schoen wrote:
> "Jerry Stuckle" wrote in message
> news:ii5eo4$ov7$1(at)news(dot)eternal-september(dot)org...
>
>> 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).
>
> Yes, I can see that, and it has been a learning experience doing the
> same thing (which should be possible) in different ways (due to the
> language differences). Now I'm having some difficulty with arrays, but I
> think I found how to do what I want, although it seems ridiculous. I
> modified an example in the manual to get this:
>
> $qAll = "SELECT * FROM tEntries";
> $result = $db->query($qAll);
> $row = array("myArray");
> $i = 0;
> while($res = $result->fetcharray(SQLITE3_BOTH)){
> if(!isset( $res['eid'] )) continue;
> $row[$i][0] = $res[0];
> $row[$i][1] = $res[1];
> $row[$i]['et'] = $res['et'];
> $i++;
> }
> print_r($row);
> $maxrow = $i-1;
> for($i=0; $i<$maxrow; $i++) {
> fwrite( $fLog, "{$row[$i][0]} {$row[$i][1]} {$row[$i]['et']}\n");
> fwrite( $fLog, "$row[$i][0] $row[$i][1] $row[$i]['et']\n");
> }
>
> I can't understand why you can assign an array element $row[$i][0] =
> $res[0]; which is a simple scalar quantity (in this case an integer),
> but when you use the same expression to write or print, it shows:
> Array[0], and the curly braces are needed to extract to actual value. I
> only found this deep in the user-supplied examples for arrays, and one
> would think this is important enough to include in the main documentation.
> http://www.php.net/manual/en/language.types.array.php
>
> I didn't see anything in the array operators:
> http://www.php.net/manual/en/language.operators.array.php
>
> It’s not in operator precedence:
> http://www.php.net/manual/en/language.operators.precedence.php
>
> I did a search for php curly braces:
> http://cowburn.info/2008/01/12/php-vars-curly-braces/
>
> And from that I found this:
> http://php.net/manual/en/language.types.string.php
>
> So, now that I know this, I can proceed. It's probably still better than
> Perl's various uses of $, %, #, and (), [], {}. Then you can throw in
> the @ operator and -> and => and all the other ASCII characters for a
> real alphabet soup. OK, I'll stop complaining now. But I feel almost
> like the victim of a joke, and I think I'll be fooled a few more times
> before I get this project working.
>
> :)
>
> Paul

You only need { ... } when expanding multi-dimensional arrays or
objects. Once inside { ... } you must follow the proper quote syntax for
key names.

You can assign a HEREDOC to a variable just like anything else and
variable expansion works there too:

$str = <<<EOF

blah blah $color_red blah blah
{temp[0][1][2]}
blah blah
EOF;


--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
Re: Heredoc print to file? Use nowdoc. [message #172129 is a reply to message #172123] Mon, 31 January 2011 12:34 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/31/2011 2:31 AM, P E Schoen wrote:
> "Jerry Stuckle" wrote in message
> news:ii5eo4$ov7$1(at)news(dot)eternal-september(dot)org...
>
>> 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).
>
> Yes, I can see that, and it has been a learning experience doing the
> same thing (which should be possible) in different ways (due to the
> language differences). Now I'm having some difficulty with arrays, but I
> think I found how to do what I want, although it seems ridiculous. I
> modified an example in the manual to get this:
>
> $qAll = "SELECT * FROM tEntries";
> $result = $db->query($qAll);
> $row = array("myArray");
> $i = 0;
> while($res = $result->fetcharray(SQLITE3_BOTH)){
> if(!isset( $res['eid'] )) continue;
> $row[$i][0] = $res[0];
> $row[$i][1] = $res[1];
> $row[$i]['et'] = $res['et'];
> $i++;
> }
> print_r($row);
> $maxrow = $i-1;
> for($i=0; $i<$maxrow; $i++) {
> fwrite( $fLog, "{$row[$i][0]} {$row[$i][1]} {$row[$i]['et']}\n");
> fwrite( $fLog, "$row[$i][0] $row[$i][1] $row[$i]['et']\n");
> }
>
> I can't understand why you can assign an array element $row[$i][0] =
> $res[0]; which is a simple scalar quantity (in this case an integer),
> but when you use the same expression to write or print, it shows:
> Array[0], and the curly braces are needed to extract to actual value. I
> only found this deep in the user-supplied examples for arrays, and one
> would think this is important enough to include in the main documentation.
> http://www.php.net/manual/en/language.types.array.php
>

You can print a scalar quantity, even if it is an array element.
However, if the element is itself an array, you can assign the array buy
not print it. If you're getting Array, you have an array, not a scalar.

But I don't see anything in your sample code which would print Array[0],
so I have no idea what you might actually be doing.

> I didn't see anything in the array operators:
> http://www.php.net/manual/en/language.operators.array.php
>
> It’s not in operator precedence:
> http://www.php.net/manual/en/language.operators.precedence.php
>
> I did a search for php curly braces:
> http://cowburn.info/2008/01/12/php-vars-curly-braces/
>
> And from that I found this:
> http://php.net/manual/en/language.types.string.php
>
> So, now that I know this, I can proceed. It's probably still better than
> Perl's various uses of $, %, #, and (), [], {}. Then you can throw in
> the @ operator and -> and => and all the other ASCII characters for a
> real alphabet soup. OK, I'll stop complaining now. But I feel almost
> like the victim of a joke, and I think I'll be fooled a few more times
> before I get this project working.
>
> :)
>
> Paul

Your biggest problem is you are still trying to equate PHP to Perl.
It's a HUGE mistake to do that (no matter what the language). Trying to
look for the differences will only confuse you.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Heredoc print to file? Use nowdoc. [message #172132 is a reply to message #172129] Mon, 31 January 2011 13:05 Go to previous messageGo to next message
P E Schoen is currently offline  P E Schoen
Messages: 86
Registered: January 2011
Karma: 0
Member
"Jerry Stuckle" wrote in message
news:ii6a9c$kmg$1(at)news(dot)eternal-september(dot)org...

> Your biggest problem is you are still trying to equate PHP to Perl. It's a
> HUGE mistake to do that (no matter what the language).
> Trying to look for the differences will only confuse you.

Well, I finally got everything working at least about as well as the
equivalent Perl script, so now I can just continue to develop this project
in PHP. I was confused about the {} operator and it seems that it is only
needed within a string. Now I have something like this:

$arr = array();
$qres = $db->query($q);
$line = 0;
while ($row = $qres->fetcharray(SQLITE3_BOTH)) {
for ($i = 0; $i < 9; $i++) {
$arr[$line][$i] = $row[$i];
}
$dtStart = date_create_from_format('Y-m-d', substr($arr[$line][4], 0,
10) );
$dowStart = $arr[$line][5];
$timeStart = substr($arr[$line][4], 11, 5);
$dtEnd = date_create_from_format('Y-m-d', substr($arr[$line][6], 0,
10) );
$dowEnd = $arr[$line][7];
$timeEnd = substr($arr[$line][6], 11, 5);
$url = $arr[$line][3];
$descr = $arr[$line][8];
$id = $arr[$line][0];
$title = $arr[$line][2];

$nowdoc .= "<p><h3>Title: $title</h3>\n Entry ID: $id\n";
$nowdoc .= "<h4>From: $dowStart, ";
$nowdoc .= date_format($dtStart, 'M d, Y');
$nowdoc .= " at $timeStart";
$nowdoc .= " to $dowEnd, ";
$nowdoc .= date_format($dtEnd, 'M d, Y') . "\n";
$nowdoc .= " at $timeEnd</h4>\n";
$nowdoc .= "URL: $url\n<br>Description: $descr<hr></p>\n";
$line++;
}

It would be better with an associative array but I'm not sure if I can loop
through with:

// eid,etype,et,eurl,sdt,sdow,edt,edow,ed (array elements)
for ($i = 'eid'; $i <= 'ed'; $i++) {
$arr[$line][$i] = $row[$i];
}

I would normally (in Delphi) do this with an "enum" with the integers 0..8
represented by eid..ed.

When I get this script cleaned up I'll put it on my website so you may be
able to see the whole thing and provide any major caveats. I am concerned
about security although the data in this database is (at present) only going
to be lists of events and outings, and I might also make a similar setup for
a sort of blog or ongoing discussion. I've learned a lot over the past
several days, and I do think PHP is the best for me to use from now on.
Thanks for the help and patience.

Paul
Re: Heredoc print to file? Use nowdoc. [message #172134 is a reply to message #172132] Mon, 31 January 2011 13:38 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 31-01-11 14:05, P E Schoen wrote:
> It would be better with an associative array but I'm not sure if I can
> loop through with:
>
> // eid,etype,et,eurl,sdt,sdow,edt,edow,ed (array elements)
> for ($i = 'eid'; $i <= 'ed'; $i++) {
> $arr[$line][$i] = $row[$i];
> }
>
> I would normally (in Delphi) do this with an "enum" with the integers
> 0..8 represented by eid..ed.

http://nl2.php.net/manual/en/control-structures.foreach.php
$a = array('eid','etype','et','eurl','sdt','sdow','edt','edow','ed');
foreach ($a as $value) {
echo $value."\n";
}

--
Luuk
Re: Heredoc print to file? Use nowdoc. [message #172135 is a reply to message #172132] Mon, 31 January 2011 15:49 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <Csy1p.9030$Y8(dot)1426(at)newsfe06(dot)iad>,
"P E Schoen" <paul(at)pstech-inc(dot)com> wrote:

> "Jerry Stuckle" wrote in message
> news:ii6a9c$kmg$1(at)news(dot)eternal-september(dot)org...
>
>> Your biggest problem is you are still trying to equate PHP to Perl. It's a
>> HUGE mistake to do that (no matter what the language).
>> Trying to look for the differences will only confuse you.
>
> Well, I finally got everything working at least about as well as the
> equivalent Perl script, so now I can just continue to develop this project
> in PHP. I was confused about the {} operator and it seems that it is only
> needed within a string. Now I have something like this:
>
> $arr = array();
> $qres = $db->query($q);
> $line = 0;
> while ($row = $qres->fetcharray(SQLITE3_BOTH)) {
> for ($i = 0; $i < 9; $i++) {
> $arr[$line][$i] = $row[$i];
> }

Hmmm, you're collecting one row at a time and putting it in a 2D array.
Seems to me that if you do fetchAll instead of fetcharray then you get
*given* a 2D array and you don't even have to declare it. I would have
thought that to be more efficient.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
Re: Heredoc print to file? Use nowdoc. [message #172137 is a reply to message #172132] Mon, 31 January 2011 16:11 Go to previous message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <Csy1p.9030$Y8(dot)1426(at)newsfe06(dot)iad>,
"P E Schoen" <paul(at)pstech-inc(dot)com> wrote:

> "Jerry Stuckle" wrote in message
> news:ii6a9c$kmg$1(at)news(dot)eternal-september(dot)org...
>
>> Your biggest problem is you are still trying to equate PHP to Perl. It's a
>> HUGE mistake to do that (no matter what the language).
>> Trying to look for the differences will only confuse you.
>
> Well, I finally got everything working at least about as well as the
> equivalent Perl script, so now I can just continue to develop this project
> in PHP. I was confused about the {} operator and it seems that it is only
> needed within a string. Now I have something like this:
>
> $arr = array();
> $qres = $db->query($q);
> $line = 0;
> while ($row = $qres->fetcharray(SQLITE3_BOTH)) {
> for ($i = 0; $i < 9; $i++) {
> $arr[$line][$i] = $row[$i];
> }
> $dtStart = date_create_from_format('Y-m-d', substr($arr[$line][4], 0,
> 10) );
> $dowStart = $arr[$line][5];

.... and furthermore, you can do like this:

$reg = $qres->fetchAll (SQLITE3_BOTH);
$num = count ($reg);

for ($i=0; $i<$num; $i++)
{
$url = $reg[$i]["eurl"];
etc ...

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Mon Nov 25 23:45:57 GMT 2024

Total time taken to generate the page: 0.06213 seconds