I have been working on a function which creates a 1 million digit value of pi but php is treating a theoretical numeric variable as a string. Below is the code I used and for now I have put a small limit on the digits but it is line 22 that wont calculate. Does anybody know how to make it so that a function inside a variable can be executed with its mathematical calculations?
<?
function realpi()
{
$pival=1;
while ($pirow<6)
{
$pirow+=1;
$pisubrow=1;
$tempval="(0.5*sqrt(2";
while ($pisubrow<$pirow)
{
$tempval.="+sqrt(2";
$pisubrow+=1;
}
$pisubrow=1;
while ($pisubrow<$pirow)
{
$tempval.=")";
$pisubrow+=1;
}
$tempval.="))";
$pival=$pival*$tempval;
var_dump($tempval);
echo "<br>";
}
$pival=$pival/2;
return $pival;
unset($pirow);
}
echo realpi();
?>
With the echo functions inside the code, the below is echoed
string(13) "(0.5*sqrt(2))"
string(21) "(0.5*sqrt(2+sqrt(2)))"
string(29) "(0.5*sqrt(2+sqrt(2+sqrt(2))))"
string(37) "(0.5*sqrt(2+sqrt(2+sqrt(2+sqrt(2)))))"
string(45) "(0.5*sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2))))))"
string(53) "(0.5*sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2)))))))"
0
All except the last line in the above box is a var_dump of the looped variable which will not perform the mathematical and function calculations. The last line is the answer that the function has got for pi. The reason why it is zero is because of line 22 where $tempval is a string. But it needs to be a mathematical string as it has functions that need to execute. Does anyone know how to execute this variable?