Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #15589

Re: Refresher for array handling???

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.php
Subject Re: Refresher for array handling???
Date 2015-07-17 02:51 +0000
Organization A noiseless patient Spider
Message-ID <mo9qjq$645$3@dont-email.me> (permalink)
References <lvbaqa189n0e1ldtgg1de1vprbjvs46es2@4ax.com>

Show all headers | View raw


On Tue, 14 Jul 2015 12:12:46 -0400, bobmct wrote:


> $frq = array(array);

> for  ( $X=$_minimum; $X<=$_maximum; $X++) {
> 	$Y = sprintf("%02d",$X);
> 	$Y1 = sprintf("%u",$X);
> 	print <<< EOD <tr bgcolor="$bgcolor">
> 	<td align="center"><img src="pix/balls/$Y.png" alt="$Y" ></td>
> EOD;
> 	for ( $z=0; $z<=7; $z++) {
> 		$Y2 = sprintf("%u",$z);
> 		print <<< EOD <td align="center">$frq[$Y1][$Y2]</td>
> EOD;
> 	}
> }

Should '$frq[$Y1][$Y2]' actually be '{$frq[$Y1][$Y2]}'

I'm not sure if array referencing in heredoc requires {} wrappers round 
the array and keys or not, but I always tend to use it. If it does need 
the {} wrappers, absence of them mean it's trying to convert $frq into a 
string on it's own, rather than a second order sub-element of the multi-
level $frq array!

If that's the case, this may be the cause of your "array to string 
conversion errors".

From:

http://php.net/manual/en/language.types.string.php

"Heredoc text behaves just like a double-quoted string, without the 
double quotes. This means that quotes in a heredoc do not need to be 
escaped, but the escape codes listed above can still be used. Variables 
are expanded, but the same care must be taken when expressing complex 
variables inside a heredoc as with strings."

and:

The most important feature of double-quoted strings is the fact that 
variable names will be expanded. See string parsing for details. 

and:

If a dollar sign ($) is encountered, the parser will greedily take as 
many tokens as possible to form a valid variable name. Enclose the 
variable name in curly braces to explicitly specify the end of the name. 

But the latter statement isn't neccesarily true, as the following test 
shows. The test code:

<?php

$arr = array( array( 'x' ) );

echo print_r($arr, TRUE);

echo "Test 1: {$arr[0][0]}\n";

echo "Test 2: $arr[0][0]\n";

produces:

Array
(
    [0] => Array
        (
            [0] => x
        )

)
Test 1: x
Test 2: Array[0]

Note that the second print appears to have output '$arr[0]' as 'Array', 
and the following '[0]' as a literal '[0]', where reading the 
documentation suggests that the parser should greedily grab the second 
index as well before trying to resolve the variable!

Expanding the test:

<?php

$arr = array( array( 'x' ) );

echo print_r($arr, TRUE);

echo "Test 1: {$arr[0][0]}\n";

echo "Test 2: $arr[0][0]\n";

echo <<< EOT

Test 3: {$arr[0][0]}

EOT;

echo <<< EOT

Test 4: $arr[0][0]

EOT;

gives:

Array
(
    [0] => Array
        (
            [0] => x
        )

)
Test 1: x
Test 2: Array[0]

Test 3: x

Test 4: Array[0]

Which suggests that heredoc also sees '$arr[0]' and '[0]' as two seperate 
things when they're not wrapped in {}.

However there is no error or warning generated, even if I include 
'error_reporting(-1);' as the first line of code.

-- 
Denis McMahon, denismfmcmahon@gmail.com

Back to comp.lang.php | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Refresher for array handling??? bobmct <r.mariotti@fdcx.net> - 2015-07-14 12:12 -0400
  Re: Refresher for array handling??? Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-07-15 15:01 +0200
    Re: Refresher for array handling??? Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-07-15 15:03 +0200
    Re: Refresher for array handling??? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-17 21:24 +0200
      Re: Refresher for array handling??? Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-07-20 16:30 +0200
        Re: Refresher for array handling??? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-20 19:45 +0200
          Re: Refresher for array handling??? Matthew Carter <m@ahungry.com> - 2015-07-20 22:36 -0400
          Re: Refresher for array handling??? Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-07-21 11:22 +0200
            Re: Refresher for array handling??? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-21 13:30 +0200
              Re: Refresher for array handling??? Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-07-21 14:03 +0200
        Re: Refresher for array handling??? Denis McMahon <denismfmcmahon@gmail.com> - 2015-07-21 01:01 +0000
          Re: Refresher for array handling??? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-21 09:07 +0200
  Re: Refresher for array handling??? "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-07-15 20:18 +0200
  Re: Refresher for array handling??? Denis McMahon <denismfmcmahon@gmail.com> - 2015-07-17 02:51 +0000

csiph-web