PHP seems to be ambiguous: which result is correct ?
PHP seems to be ambiguous: which result is correct ?
am 02.12.2007 12:09:09 von BOUTM13
Now for the really kinky thing: running the following routine on my
localhost(apache 2.2.3,PHP5), I get
2 2 2 2 2 2
2 1 1 1 1 1
2 2 1 1 1 1
2 2 2 1 1 1
2 2 2 2 1 1
2 2 2 2 2 1
Running it through the Eclipse PHP Zend debugger I get (which, in my
opinion, is right):
2 4 4 4 4 4
4 2 4 4 4 4
4 4 2 4 4 4
4 4 4 2 4 4
4 4 4 4 2 4
4 4 4 4 4 2
$k = array(1=> // upper triangular matrix of 1's
array(1=>1,1,1,1,1,1),
array(2=>1,1,1,1,1),
array(3=>1,1,1,1),
array(4=>1,1,1),
array(5=>1,1),
array(6=>1));
// I try to mirror k about diagonal, while doubling:
foreach($k as $i=>& $_k)
foreach($_k as $j=>&$__k)
{
$__k *= 2;
if ($i != $j)
$k[$j][$i ] = $__k;
}
// however:
for ($i=1; $i <= 6; $i++)
{
for ($j=1; $j <= 6; $j++)
echo $k[$i][$j]." ";
echo "
";
}
?>
Re: PHP seems to be ambiguous: which result is correct ?
am 02.12.2007 13:37:45 von luiheidsgoeroe
On Sun, 02 Dec 2007 12:09:09 +0100, Osiris wrote:
> Now for the really kinky thing: running the following routine on my
> localhost(apache 2.2.3,PHP5), I get
> 2 2 2 2 2 2
> 2 1 1 1 1 1
> 2 2 1 1 1 1
> 2 2 2 1 1 1
> 2 2 2 2 1 1
> 2 2 2 2 2 1
> Running it through the Eclipse PHP Zend debugger I get (which, in my
> opinion, is right):
> 2 4 4 4 4 4
> 4 2 4 4 4 4
> 4 4 2 4 4 4
> 4 4 4 2 4 4
> 4 4 4 4 2 4
> 4 4 4 4 4 2
This is exactly what I get using 5.2.4. What exact version are you using?
And is this all the code?
--
Rik Wasmus
Re: PHP seems to be ambiguous: which result is correct ?
am 02.12.2007 13:38:27 von luiheidsgoeroe
On Sun, 02 Dec 2007 13:37:45 +0100, Rik Wasmus
wrote:
> On Sun, 02 Dec 2007 12:09:09 +0100, Osiris wrote:
>
>> Now for the really kinky thing: running the following routine on my
>> localhost(apache 2.2.3,PHP5), I get
>> 2 2 2 2 2 2
>> 2 1 1 1 1 1
>> 2 2 1 1 1 1
>> 2 2 2 1 1 1
>> 2 2 2 2 1 1
>> 2 2 2 2 2 1
>> Running it through the Eclipse PHP Zend debugger I get (which, in my
>> opinion, is right):
>> 2 4 4 4 4 4
>> 4 2 4 4 4 4
>> 4 4 2 4 4 4
>> 4 4 4 2 4 4
>> 4 4 4 4 2 4
>> 4 4 4 4 4 2
>
> This is exactly what I get using 5.2.4. What exact version are you
> using? And is this all the code?
Euhm, to be clear: the last one is what I get.
--
Rik Wasmus
Re: PHP seems to be ambiguous: which result is correct ?
am 02.12.2007 23:29:50 von BOUTM13
On Sun, 02 Dec 2007 13:37:45 +0100, Rik Wasmus wrote:
> On Sun, 02 Dec 2007 12:09:09 +0100, Osiris wrote:
>
>> Now for the really kinky thing: running the following routine on my
>> localhost(apache 2.2.3,PHP5), I get
>> 2 2 2 2 2 2
>> 2 1 1 1 1 1
>> 2 2 1 1 1 1
>> 2 2 2 1 1 1
>> 2 2 2 2 1 1
>> 2 2 2 2 2 1
>> Running it through the Eclipse PHP Zend debugger I get (which, in my
>> opinion, is right):
>> 2 4 4 4 4 4
>> 4 2 4 4 4 4
>> 4 4 2 4 4 4
>> 4 4 4 2 4 4
>> 4 4 4 4 2 4
>> 4 4 4 4 4 2
>
> This is exactly what I get using 5.2.4. What exact version are you using?
> And is this all the code?
>
That is all the code. Just cut and paste and run.
Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4 PHP/5.2.0-8+etch7 mod_perl/2.0.2 Perl/v5.8.8
Linux echo 2.6.18-5-k7 #1 SMP Wed Oct 3 00:47:27 UTC 2007 i686
Re: PHP seems to be ambiguous: which result is correct ?
am 03.12.2007 01:18:13 von Csaba2000
Osiris wrote:
> Now for the really kinky thing: running the following routine on my
> localhost(apache 2.2.3,PHP5), I get
> 2 2 2 2 2 2
> 2 1 1 1 1 1
> 2 2 1 1 1 1
> 2 2 2 1 1 1
> 2 2 2 2 1 1
> 2 2 2 2 2 1
> Running it through the Eclipse PHP Zend debugger I get (which, in my
> opinion, is right):
> 2 4 4 4 4 4
> 4 2 4 4 4 4
> 4 4 2 4 4 4
> 4 4 4 2 4 4
> 4 4 4 4 2 4
> 4 4 4 4 4 2
....
> foreach($k as $i=>& $_k)
> foreach($_k as $j=>&$__k)
> {
> $__k *= 2;
> if ($i != $j)
> $k[$j][$i ] = $__k;
> }
....
I believe there may have been some bug fixes that
affected this behaviour. Specifically, things similar to:
http://bugs.php.net/bug.php?id=29687
http://bugs.php.net/bug.php?id=35106
though I'm not sure that either of them describes your
situation. In any case, I'd expect that the top
example is an earlier PHP version that has since been
fixed.
I am getting the second set of results (with the 4's)
with PHP 5.2.4 (Aug 30, 2007), Win XP Pro. From my read
of the output, what is happening in the top example is
that the reference (&$_k or &$__k) is no longer referring
to the actual row or cell, but rather to a copy, hence it
does not get doubled on the upper triangle, after the first
pass. Since you're explicitly setting the lower triangle
values, those do reflect a doubling.
To get all 2s, except for 1s on the diagonal, you could,
in keeping with the above style, use (and this should
still produce the discrepancy between your two systems):
foreach($k as $i=>&$_k) {
foreach($_k as $j=>&$__k)
if ($i < $j) {
$__k *= 2;
$k[$j][$i ] = $__k; } }
Finally, just in case you weren't aware, you're 2nd row
starts off life as:
2 => 1, 3 => 1, 4 => 1, 5 => 1, 6 => 1
When you then insert a 2, a la $k[2][1]=$__k
then the resulting row does NOT look like:
1 => 2, 2 => 1, 3 => 1, 4 => 1, 5 => 1, 6 => 1
Instead, it looks like:
2 => 1, 3 => 1, 4 => 1, 5 => 1, 6 => 1, 1 => 2
So when you iterate that with a foreach, the
inserted entry will be last and not first.
The point being that the order numeric indeces
actually appear in PHP arrays is not always
tied to their value, unless they were entered
sequentially from 0 in the first place.
Csaba Gabor from Vienna