PHP Examples

This bit of code is an auto emailer... in real life it must be enclosed by the PHP entrance and exit sequences

$today = date('l F j') . ", " . date('Y');
$to = $_POST["agent"];
$subject = "Service Request dated " . $today;
$body = "Message from " . $_POST["FN"] . " " . $_POST["LN"] . " , policy number " . $_POST["policynum"] . ".
" . $_POST["message"];
mail($to, $subject, $body);


This long secion of PHP code is an external page that is imported inside the head of the program. It contains a group of
computation functions, array lookups I wrote to make the calculations necessary to provide an insurance quote. Again it must be enclosed by the PHP entrance and exit sequences


/* the get_daily rate function returns the total from the physical damages and theft daily rates
tables for a specific position cooresponding to the same position in the insured sum table.
The 4.92 is the total of the four rate catagories that don't change since there is only a
single policy offerred. */

function get_daily_rate($cash_value, $lr) {

//physical damages rate chart
$pdrate = array('0.33',
'0.55',
'0.71',
'0.87',
'1.03',
'1.17',
'1.31',
'1.45',
'1.59',
'1.70',
'1.81',
'1.94',
'2.06',
'2.12',
'2.21',
'2.31',
'2.40',
'2.48',
'2.58',
'2.67',
'2.75',
'2.84',
'2.94',
'3.02',
'3.10',
'3.21',
'3.29',
'3.38',
'3.48',
'3.56',
'3.65',
'3.75',
'3.84',
'3.92',
'4.00',
'4.09',
'4.20',
'4.29',
'4.38');

//theft rate chart
$ttrate = array('0.28',
'0.37',
'0.46',
'0.55',
'0.64',
'0.68',
'0.75',
'0.82',
'0.89',
'0.94',
'0.99',
'1.04',
'1.08',
'1.14',
'1.20',
'1.24',
'1.28',
'1.31',
'1.36',
'1.40',
'1.44',
'1.48',
'1.52',
'1.55',
'1.60',
'1.64',
'1.68',
'1.72',
'1.76',
'1.79',
'1.84',
'1.88',
'1.92',
'1.96',
'2.00',
'2.04',
'2.08',
'2.12',
'2.16');

// Get pointer into the rate tables --0 to 38-- and the calc area above
$ptr = $cash_value / 500; // enter with cash value at $500 steps
$ptr = $ptr - 2; // set for zero based table

if ($ptr < 38) {
return $pdrate[$ptr] + $ttrate[$ptr] + $lr; // take from table
} else {
$pdrate = (($ptr - 38) * .08) + 4.38; // add to end of table
$ttrate = (($ptr - 38) * .04) + 2.16;
return $pdrate + $ttrate;
}
}

function cash_value($cv) {
$a = trim($cv); // remove any whitespace
$b = remove_commas($a); // take out commas if any
$v = round($b); // make whole number, cents rounded up
$d = create_steps($v); // create $500 steps, adjust up to nearest 500
return $d;
}

function remove_commas($cv) {
$l = strlen($cv); // length of string to check
$i = 0; // char counter
$s = ""; // new string without commas
do {
$c = substr($cv, $i, 1); // find single char at 'i' position
if ($c != ",") { // if not a comma, add char to new string
$s = $s . $c;
}
$i++;
}
while ($i < $l); // loop counter
return $s; // return string minus commas
}

function create_steps($cv) { //return $500 steps...1000 to 1499 = 1500. 1500 to 1999 = 2000
$j = strlen($cv); // number of chars in cash value
if ($j < 4){
$cve = "1000"; // if cash value entered is anything under 1000, return $1000 as base step
return $cve;
}
$hd = "";
$i = $j - 3; // point to third from end char to get hundreds value
While ($i < $j) {
$c = substr($cv, $i, 1);
$hd = $hd . $c;
$i++;
}
// $hd now has the hundreds digits, get the thousands
$th = "";
$j = strlen($cv); // number of chars in cash value
$j = $j - 3; //point to third from end char
$i = 0;
While ($i < $j) {
$c = substr($cv, $i, 1);
$th = $th . $c;
$i++;
}
// $th now has the thousands digits, adjust upwards for $500 steps
if ($hd <'500') {
if ($hd > '000') {
$hd = '500';
}
}else{
if ($hd > '500') {
$hd = '000';
$th = $th + 1;
}
}
$cve = $th . $hd; // put the hundreds and thousands together again
return $cve; // return adjusted $500 step value
}

function get_discount($d) {
// array of discounts between 30 and 180 days. Over 180 becomes standard 90%
$disct = array('0.10',
'0.10',
'0.10',
'0.10',
'0.10',
'0.10',
'0.10',
'0.10',
'0.10',
'0.10',
'0.131',
'0.131',
'0.131',
'0.131',
'0.131',
'0.131',
'0.131',
'0.131',
'0.131',
'0.131',
'0.162',
'0.162',
'0.162',
'0.162',
'0.162',
'0.162',
'0.162',
'0.162',
'0.162',
'0.162',
'0.1925',
'0.1925',
'0.1925',
'0.1925',
'0.1925',
'0.1925',
'0.1925',
'0.1925',
'0.1925',
'0.1925',
'0.2215',
'0.2215',
'0.2215',
'0.2215',
'0.2215',
'0.2215',
'0.2215',
'0.2215',
'0.2215',
'0.2215',
'0.25',
'0.25',
'0.25',
'0.25',
'0.25',
'0.25',
'0.25',
'0.25',
'0.25',
'0.25',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.2775',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.355',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425',
'0.425');

return $disct[$d];
}

function under_30_comp($dc, $d) {
$total_cost_cp = $dc * $d; // days X daily cost
$total_cost_cp = $total_cost_cp * 1.1; // 10% tax
return ($total_cost_cp + 10); // $10 policy fee
}

function one_to_six_comp($dc, $d) {
$total_cost_cp = $dc * $d; // days X daily cost
$total_cost_cp = $total_cost_cp - ($total_cost_cp * get_discount($d - 30)); // variable discount
$total_cost_cp = $total_cost_cp * 1.1; // 10% tax
return ($total_cost_cp + 10); // $10 policy fee
}

function annual_comp($dc) {
$an_cp = $dc * 365;
$an_cp = $an_cp - ($an_cp * 0.9); // 90% discount
$an_cp = $an_cp * 1.1; // 10% tax
return ($an_cp + 25); // $25 policy fee
}

function six_month_comp($dc) {
$an_cp = $dc * 180;
$an_cp = $an_cp - ($an_cp * 0.9); // 90% discount
$an_cp = $an_cp * 1.1; // 10% tax
return ($an_cp + 25); // $25 policy fee
}

function annual_liability($lc) {
$an_lb = $lc * 365;
$an_lb = $an_lb - ($an_lb * 0.9); // 90% discount
$an_lb = $an_lb * 1.1; // 10% tax
return ($an_lb + 25); // $25 policy fee
}

function six_month_liability($lc) {
$an_lb = $lc * 180;
$an_lb = $an_lb - ($an_lb * 0.9); // 90% discount
$an_lb = $an_lb * 1.1; // 10% tax
return ($an_lb + 25); // $25 policy fee
}

function under_30_liability($d, $lc) {
$total_cost_lb = $lc * $d; // total liability
$total_cost_lb = $total_cost_lb * 1.1; // 10% tax
return ($total_cost_lb + 10); // $10 policy fee
}

function one_to_six_liability($d, $lc) {
$total_cost_lp = $lc * $d; // total liability
$total_cost_lp = $total_cost_lp - ($total_cost_lp * get_discount($d - 30)); // variable discount
$total_cost_lp = $total_cost_lp * 1.1; // 10% tax
return ($total_cost_lp + 10); // $10 policy fee
}

// $d1, $d2 should be in this format '12/3/2008'
function number_of_days ($d1, $d2) {
$date1 = strtotime($d1);
$date2 = strtotime($d2);
$diff = $date2 - $date1;
return round($diff/86400);
}

// time string compare must be in this format '11:00 am' or '3:00 pm'
function time_order($timestr) {
$timeofday = array('12:00 am',
'1:00 am',
'2:00 am',
'3:00 am',
'4:00 am',
'5:00 am',
'6:00 am',
'7:00 am',
'8:00 am',
'9:00 am',
'10:00 am',
'11:00 am',
'12:00 pm',
'1:00 pm',
'2:00 pm',
'3:00 pm',
'4:00 pm',
'5:00 pm',
'6:00 pm',
'7:00 pm',
'8:00 pm',
'9:00 pm',
'10:00 pm',
'11:00 pm');

$tmcode = 0;
for ($i=0; $i<=23; $i++) {
$k = strcmp($timestr, $timeofday[$i]);
if ($k == 0) {
$tmcode = $i;
}
}
return $tmcode;
}