<?php
/*
Functions
© Charles Chandler
http://qdl.scs-inc.us/?top=12801
*/
error_reporting(E_ALL);
function Random($minVal, $maxVal, $precision = 3) {
$powPrec = pow(10, $precision);
$randomFloat = mt_rand(0, $powPrec) / $powPrec;
return $min + ($randomFloat * ($maxVal - $minVal));
}
function Pt($x, $y, $z) {
// just a shorthand for building the array
return array('x' => floatval($x), 'y' => floatval($y), 'z' => floatval($z));
}
function Gravity_n($m1_kg, $m2_kg, $dist_m) {
// Given two masses (in kilograms), and the distance between them
// (in meters), this returns the gravitational force (in Newtons).
return (6.67384 * pow(10, -11) * $m1_kg * $m2_kg) / ($dist_m * $dist_m);
}
function Electricity_n($num_e_1, $num_e_2, $dist_m) {
// Given two quantities of elementary charges, and the distance between
// them (in meters), this returns the electric force (in Newtons).
$c1 = $num_e_1 * e; // convert elementary charges to coulombs
$c2 = $num_e_2 * e; // convert elementary charges to coulombs
return (8.988 * pow(10, 9) * $c1 * $c2) / ($dist_m * $dist_m);
}
function ScientificNotation($num, $prec) {
$minus = ($num < 0) ? '−' : '';
$num = abs($num);
$exp = NULL;
if ($num) {
$exp = round(log($num, 10));
if (abs($exp) > 2) {
$num = $num / pow(10, $exp);
if ($num < 1) { $num *= 10; $exp -= 1; }
if ($exp < 0) $exp = '−'.(abs($exp));
$exp = ' × 10<sup>'.$exp.'</sup>';
} else $exp = NULL;
}
return $minus.round($num, $prec).$exp;
}
function lpad($theString, $theLength, $theCharacter = ' ') {
return str_pad($theString, $theLength, $theCharacter, STR_PAD_LEFT);
}
function DirList($directory) {
$directory = trim($directory, '/');
$results = array();
if (file_exists($directory)) {
$dir = opendir($directory);
while ($file = readdir($dir)) {
if (($file != '.') and ($file != '..') and ($file != '.DS_Store') and ($file != '__MACOSX')) {
$results[] = $file;
}
}
closedir($dir);
}
natcasesort($results);
return $results;
}
function VarExport($var) {
ob_start();
var_export($var);
return ob_get_clean();
}
function file_put_array($file, $arrayName) {
global $$arrayName;
$output = '<?php'."\n\n".'$'.$arrayName.' = '.VarExport($$arrayName).";\n\n?>\n";
file_put_contents($file, $output);
}
function StripOneTailer($theString, $theTailer) {
$tailerLength = strlen($theTailer);
if (substr($theString, -$tailerLength, $tailerLength) == $theTailer) {
$theString = substr($theString, 0, strlen($theString) - $tailerLength);
}
return $theString;
}
function ErrorHandler($errNum, $errMsg, $fileName, $lineNum, $vars) {
$errTypes = array (
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parsing Error',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'QDL Error',
E_USER_WARNING => 'QDL Warning',
E_USER_NOTICE => 'QDL Dev Notice',
E_STRICT => 'Runtime Notice',
E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
//E_DEPRECATED => 'Deprecated Feature',
//E_USER_DEPRECATED => 'User Deprecated Feature',
);
$pieces = explode('/', $fileName);
echo '
<table>
<tr><td>error type: </td><td>'.$errTypes[$errNum].'</td></tr>
<tr><td>message: </td><td>'.$errMsg.'</td></tr>
<tr><td>file name: </td><td>'.end($pieces).'</td></tr>
<tr><td>line num: </td><td>'.$lineNum.'</td></tr>
</table>
';
exit();
}
set_error_handler('ErrorHandler');
?>
|