Is this code possible?
A stupid question but I am having a problem understanding this.
Assuming $product_quality[$i] and $saleprice[$i] are already populated with values.
Passing an array to a function without specifying its key.
Please just follow the $product_value[$i] or $player_value
for ($i=0;$i<5;$i++)
{
$product_value[$i]=($product_quality[$i]/$saleprice[$i]);
}
$trial_x=find_four($product_value);
function find_four($player_value)
{
$trial_x=2;
$h=5;
$slope=1;
while(abs($h)>0.01){
$delta_y=total_value($player_value, $trial_x+0.01)-total_value($player_value, $trial_x)
$slope=100*$delta_y;
$h=-(total_value($player_value, $trial_x)-4)/$slope;
$trial_x=$trial_x+$h;
}
return ($trial_x);
}
function total_value($player_value, $x)
{
$total_value=0;
for ($i=0;$i<5;$i++)
{
$total_value+=Cum_Norm($x,$player_value[$i],$sd[$i]);
}
return($total_value);
}
function Cum_Norm($x, $mean, $sd)
{
echo $x."****1****<br/><hr/>";
echo $mean."Should be empty?***2****<br/><hr/>";
echo $sd."*****3*****<br/><hr/>";
$z=($x-$mean)/$sd;
echo $z."*****4*****<br/><hr/>";
$b1=0.31938;
$b2=-0.35656;
$b3=1.78148;
$b4=-1.82126;
$b5=1.33027;
$c=0.3989423;
if ($z>=0)
{
$t=1/(1+0.2316419*$z);
echo $t."*****5*****<br/><hr/>";
return (1.0 - $c * exp( -$z * $z / 2 ) * $t * ( $t *( $t * ( $t * ( $t * $b5 + $b4 ) + $b3 ) + $b2 ) + $b1 ));
}
else
{
$t=1/(1-0.2316419*$z);
echo $t."*****5*****<br/><hr/>";
return ($c * exp( -$z * $z / 2 ) * $t * ( $t *( $t * ( $t * ( $t * $b5 + $b4 ) + $b3 ) + $b2 ) + $b1 ));
}
}
The whole array
$product_valueis passed as a full array to the functionfind_four(). However,find_four()does not directly make use of the values in the$product_valuearray. Instead, the complete array is passed next to the functiontotal_value().total_value()receives the whole array (called$player_valuein scope of the functionfind_four()) and performs calculations using all elements of the array.total_value()receives the array and uses its elements inside aforloop.It isn’t totally clear from your question what you don’t understand, but it seems to be the part about passing a whole array rather than individual values. An array can be passed to a function, and indeed all the built-in PHP
array_*()functions depend on that behavior.By default, PHP passes arrays between functions by value rather than by reference, so the function receives a copy of the original array rather than the original. Any modifications the function makes to the array passed in are local modifications only.
Arrays (like any values) can be passed by reference as in
function_call(&$array), so modifications insidefunction_call()will modify the original array passed to it. Certain built-ins likesort()accept an array reference, therefore acting on the original array passed in without needing to return it and assign it back to a variable.