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 ));  
}
 }
share|improve this question

feedback

1 Answer

up vote 3 down vote accepted

The whole array $product_value is passed as a full array to the function find_four(). However, find_four() does not directly make use of the values in the $product_value array. Instead, the complete array is passed next to the function total_value().

total_value() receives the whole array (called $player_value in scope of the function find_four()) and performs calculations using all elements of the array. total_value() receives the array and uses its elements inside a for loop.

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 inside function_call() will modify the original array passed to it. Certain built-ins like sort() accept an array reference, therefore acting on the original array passed in without needing to return it and assign it back to a variable.

$input_array = array(1,2,3,4,5);

// Pass the whole array to a function (by value)
function showArray($input_array) {
  print_r($input_array);
}

showArray($input_array);
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
share|improve this answer
Thank you I was aware this question might be quite difficult to comprehend what I was looking for but you managed to answer my concerns. I was wondering if by passing just the $product_value array to a function, would the function have access to all values stored in the array ie $product_value[$i] where $i is between 0 and 4 inclusive. – NeverPhased Jan 31 at 18:13
feedback

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.