PHP seems to be ambiguous: which result is correct ?

This is a discussion on PHP seems to be ambiguous: which result is correct ? within the PHP Language forums, part of the PHP Programming Forums category; Now for the really kinky thing: running the following routine on my localhost(apache 2.2.3,PHP5), I get ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-02-2007
Osiris
 
Posts: n/a
Default PHP seems to be ambiguous: which result is correct ?

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

<?php
$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 "<BR>";
}

?>
Reply With Quote
  #2 (permalink)  
Old 12-02-2007
Rik Wasmus
 
Posts: n/a
Default Re: PHP seems to be ambiguous: which result is correct ?

On Sun, 02 Dec 2007 12:09:09 +0100, Osiris <et57@hotmail.com> 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
Reply With Quote
  #3 (permalink)  
Old 12-02-2007
Rik Wasmus
 
Posts: n/a
Default Re: PHP seems to be ambiguous: which result is correct ?

On Sun, 02 Dec 2007 13:37:45 +0100, Rik Wasmus
<luiheidsgoeroe@hotmail.com> wrote:

> On Sun, 02 Dec 2007 12:09:09 +0100, Osiris <et57@hotmail.com> 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
Reply With Quote
  #4 (permalink)  
Old 12-02-2007
Osiris
 
Posts: n/a
Default Re: PHP seems to be ambiguous: which result is correct ?

On Sun, 02 Dec 2007 13:37:45 +0100, Rik Wasmus wrote:

> On Sun, 02 Dec 2007 12:09:09 +0100, Osiris <et57@hotmail.com> 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
Reply With Quote
  #5 (permalink)  
Old 12-03-2007
Csaba Gabor
 
Posts: n/a
Default Re: PHP seems to be ambiguous: which result is correct ?

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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:03 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0