Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #11024 > unrolled thread
| Started by | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| First post | 2011-01-30 23:44 -0500 |
| Last post | 2011-01-31 06:32 -0500 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.php
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Heredoc print to file? Use nowdoc. Jerry Stuckle <jstucklex@attglobal.net> - 2011-01-30 23:44 -0500
Re: Heredoc print to file? Use nowdoc. Tim Streater <timstreater@waitrose.com> - 2011-01-31 15:49 +0000
Re: Heredoc print to file? Use nowdoc. Luuk <Luuk@invalid.lan> - 2011-01-31 14:38 +0100
Re: Heredoc print to file? Use nowdoc. Jerry Stuckle <jstucklex@attglobal.net> - 2011-01-31 07:34 -0500
Re: Heredoc print to file? Use nowdoc. "P E Schoen" <paul@pstech-inc.com> - 2011-01-31 08:05 -0500
Re: Heredoc print to file? Use nowdoc. Tim Streater <timstreater@waitrose.com> - 2011-01-31 16:11 +0000
Re: Heredoc print to file? Use nowdoc. "P E Schoen" <paul@pstech-inc.com> - 2011-01-31 02:31 -0500
Re: Heredoc print to file? Use nowdoc. Norman Peelman <npeelman@cfl.rr.com> - 2011-01-31 06:32 -0500
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2011-01-30 23:44 -0500 |
| Subject | Re: Heredoc print to file? Use nowdoc. |
| Message-ID | <ii5eo4$ov7$1@news.eternal-september.org> |
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.string.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@attglobal.net
==================
[toc] | [next] | [standalone]
| From | Tim Streater <timstreater@waitrose.com> |
|---|---|
| Date | 2011-01-31 15:49 +0000 |
| Message-ID | <timstreater-9ADD0B.15494231012011@news.individual.net> |
| In reply to | #11024 |
In article <Csy1p.9030$Y8.1426@newsfe06.iad>,
"P E Schoen" <paul@pstech-inc.com> wrote:
> "Jerry Stuckle" wrote in message
> news:ii6a9c$kmg$1@news.eternal-september.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
[toc] | [prev] | [next] | [standalone]
| From | Luuk <Luuk@invalid.lan> |
|---|---|
| Date | 2011-01-31 14:38 +0100 |
| Message-ID | <tjqi18-6gk.ln1@luuk.invalid.lan> |
| In reply to | #11024 |
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
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2011-01-31 07:34 -0500 |
| Message-ID | <ii6a9c$kmg$1@news.eternal-september.org> |
| In reply to | #11024 |
On 1/31/2011 2:31 AM, P E Schoen wrote:
> "Jerry Stuckle" wrote in message
> news:ii5eo4$ov7$1@news.eternal-september.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@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | "P E Schoen" <paul@pstech-inc.com> |
|---|---|
| Date | 2011-01-31 08:05 -0500 |
| Message-ID | <Csy1p.9030$Y8.1426@newsfe06.iad> |
| In reply to | #11099 |
"Jerry Stuckle" wrote in message
news:ii6a9c$kmg$1@news.eternal-september.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
[toc] | [prev] | [next] | [standalone]
| From | Tim Streater <timstreater@waitrose.com> |
|---|---|
| Date | 2011-01-31 16:11 +0000 |
| Message-ID | <timstreater-EA0207.16110231012011@news.individual.net> |
| In reply to | #11231 |
In article <Csy1p.9030$Y8.1426@newsfe06.iad>,
"P E Schoen" <paul@pstech-inc.com> wrote:
> "Jerry Stuckle" wrote in message
> news:ii6a9c$kmg$1@news.eternal-september.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
[toc] | [prev] | [next] | [standalone]
| From | "P E Schoen" <paul@pstech-inc.com> |
|---|---|
| Date | 2011-01-31 02:31 -0500 |
| Message-ID | <LCt1p.68620$7p5.63929@newsfe21.iad> |
| In reply to | #11024 |
"Jerry Stuckle" wrote in message
news:ii5eo4$ov7$1@news.eternal-september.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
[toc] | [prev] | [next] | [standalone]
| From | Norman Peelman <npeelman@cfl.rr.com> |
|---|---|
| Date | 2011-01-31 06:32 -0500 |
| Message-ID | <ii66kp$eo5$1@news.eternal-september.org> |
| In reply to | #11162 |
P E Schoen wrote:
> "Jerry Stuckle" wrote in message
> news:ii5eo4$ov7$1@news.eternal-september.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?-
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web