Re: Heredoc print to file? Use nowdoc. [message #172125 is a reply to message #172123] |
Mon, 31 January 2011 11:32 |
Norman Peelman
Messages: 126 Registered: September 2010
Karma:
|
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?-
|
|
|