php-src
php-src copied to clipboard
unexpected results stored depending on variables
Description
The following code:
<?php
$GTB = array(
1 => 7,
2 => 3,
3 => 5,
4 => 1,
5 => 6,
6 => 2,
7 => 4,
8 => 0);
$BTG = array(
7 => 1,
3 => 2,
5 => 3,
1 => 4,
6 => 5,
2 => 6,
4 => 7,
0 => 8);
//case 1
$u = 3;
$d = 2;
echo "u: $u, d: $d\n";
$up = $BTG[ ( (($GTB[$u] & 3) << 1) | (($GTB[$d] & 4) >> 2) ) ];
$dn = $BTG[ ( (($GTB[$u] & 1) << 2) | (($GTB[$d] & 6) >> 1) ) ];
echo "case 1 result: $up $dn\n\n";
//case 2
$u = 3;
$d = 2;
echo "u: $u, d: $d\n";
$u = $BTG[ (($GTB[$u] & 3) << 1) | (($GTB[$d] & 4) >> 2) ];
$d = $BTG[ (($GTB[$u] & 1) << 2) | (($GTB[$d] & 6) >> 1) ];
echo "case 2 result: $u $d\n\n";
//case 3
$a = 3;
$b = 2;
echo "a: $a, b: $b\n";
$a = $BTG[ (($GTB[$a] & 3) << 1) | (($GTB[$b] & 4) >> 2) ];
$b = $BTG[ (($GTB[$a] & 1) << 2) | (($GTB[$b] & 6) >> 1) ];
echo "case 3 result: $a $b\n\n";
//case 4
$ab = 3;
$cd = 2;
echo "ab: $ab, cd: $cd\n";
$ab = $BTG[ (($GTB[$ab] & 3) << 1) | (($GTB[$cd] & 4) >> 2) ];
$cd = $BTG[ (($GTB[$ab] & 1) << 2) | (($GTB[$cd] & 6) >> 1) ];
echo "case 4 result: $ab $cd\n\n";
//case 5
//switching input and output variables
$a = 3;
$b = 2;
echo "a: $a, b: $b\n";
$b = $BTG[ (($GTB[$a] & 3) << 1) | (($GTB[$b] & 4) >> 2) ];
$a = $BTG[ (($GTB[$a] & 1) << 2) | (($GTB[$b] & 6) >> 1) ];
echo "case 5 result: $b $a\n\n";
/*
In case 1 and 5,
Input variables and output variables are not the same in order and it gives me correct results.
In case 2 through 4,
Input variables are used as output variables in the same order and it gives me wrong results.
I only tested this on PHP 5.3.13p1 and PHP 7.3.33 and CentOS and Ubuntu, but I guess all php versions have the same problem regardless of platforms.
This is very unexpected and silly.
*/
?>
Resulted in this output:
u: 3, d: 2
case 1 result: 6 3
u: 3, d: 2
case 2 result: 6 4
a: 3, b: 2
case 3 result: 6 4
ab: 3, cd: 2
case 4 result: 6 4
a: 3, b: 2
case 5 result: 6 3
But I expected this output instead:
u: 3, d: 2
case 1 result: 6 3
u: 3, d: 2
case 2 result: 6 3
a: 3, b: 2
case 3 result: 6 3
ab: 3, cd: 2
case 4 result: 6 3
a: 3, b: 2
case 5 result: 6 3
PHP Version
PHP 7.3.33
Operating System
Ubuntu 18.04.6 LTS
You're overwriting the values of $u, $a, $ab, and $b in the middle of your calculations. Case 5 only "works" because the math happens to produce the same value.