Nathan
Nathan

Reputation: 1

If amount = number in field then display X value

Writing a small php calculator form to display the premium price for an input numeral amount.

unfortunately there is no static formula for the scaling of cost. So I attempted if/else echo statements for the data ranges

if(
    $policyamount<="10000". print("35\.00").
    $policyamount<="15000". print("42\.00").

And so on. Am I approaching this correctly, or is this a case of paranoid Monday morning?

Upvotes: 0

Views: 133

Answers (2)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

Personally, I'd define an array like this:

$prices = array(
    10000 => 35,
    15000 => 42,
    // define more price points here
);

Then, to display the price:

$copy = $prices;
foreach($copy as $k=>$v) {
    if( $policyamount <= $k) {
        echo number_format($v,2);
        break; // end the loop
    }
}

Upvotes: 1

Ibu
Ibu

Reputation: 43850

Let's start with the correct syntax first

if($policyamount<=10000)
    echo "35.00";
elseif ($policyamount<=15000)
    echo "42.00";

Upvotes: 1

Related Questions