For the above "distance" function to work properly, you must correct it as follows:
$d = 0.0; // must be moved before the for loop
$d += $c2*$c2 - $c1*$c1; // must be replaced by $d += ($c2 - $c1) ** 2 ;
Another thing: in the "userErrorHandler" function, the variables $vars and $user_errors are declared but not used.