-1

At first im beginner. I want to make a PHP function to calculate the discount for the pizza order. I get the data from the form.

$pizzatotal = $amount * $pizzaprice; // 4500
$realage = $currentyear - $year; //65
$cashDiscount = 2; // 2% discount if pay with cash
$yearDiscount = 3; // 3% discount if older than 60y
$payment = 1; // checkbox 1 paying with cash, 2 paying with card

I coded this function but not working fine, and i think have easiest way to calculate the discount.

echo sum($pizzatotal,$cashDiscount,$yearDiscount,$realage,$payment);

function sum($pizzatotal,$cashDiscount,$yearDiscount,$realage,$payment) {

  if ($realage >='60' && $payment ='1' ) {

    return $pizzatotal - ($pizzatotal * (($yearDiscount + $cashDiscount) / 100));
  }

  if ($realage >='60' && $payment ='2') {

    return $pizzatotal - ($pizzatotal * ($yearDiscount / 100));
  
  
  }


  if ($realage < '60' AND $payment ='1') {

    return $pizzatotal - ($pizzatotal * ($cashDiscount / 100));
  
  }

  if ($realage < '60' AND $payment ='2') {

    return $pizzatotal = $pizzatotal;
  
  }
}

Someone can help me to find other logic to make this function working good?

mrmiaki
  • 21
  • 3
  • 1
    First of all, `=` is not a comparison operator, it's an assignment operator. You need to use `==`. Next, can the discounts be combined? What is your expected result for the data that you have provided? Also, `return $pizzatotal = $pizzatotal;` is not needed, you can just `return $pizzatotal` to keep the original value. – El_Vanja Nov 25 '20 at 14:01
  • ah == maded it to work good. – mrmiaki Nov 25 '20 at 14:09
  • 1
    To keep it simple and without changing too much of your code or assuming anything, here's a cleaner way of doing the logic (untested): function sum($pizzatotal,$cashDiscount,$yearDiscount,$realage,$payment) { $discount = 0; $discount += $realage >= 60 ? $yearDiscount : 0; //If 60+, add age discount $discount += $payment == 1 ? $cashDiscount: 0; //If paying cash, add cash discount return $pizzatotal - ($pizzatotal * ($discount / 100)); } – abrosis Nov 25 '20 at 14:12
  • 1
    @abrosis Comments really aren't suitable for multiline code. Form an appropriate answer, test the logic and write at least a short explanation what your changes accomplish and how your solution works. OP is clearly a beginner and needs guidance. – El_Vanja Nov 25 '20 at 14:14
  • 1
    @El_Vanja I had written a fuller answer, but answers closed before I got to post. I was just hoping to send the suggestion to the dev in case it helped. – abrosis Nov 25 '20 at 14:53
  • yes, solved! thank you guys! – mrmiaki Nov 25 '20 at 15:31

1 Answers1

0

Like El_Vanja write and $realage < '60' - '60' do not need this: ''.

And please keep your code unified, not like this:

$realage >='60' && $payment ='1' $realage < '60' AND $payment ='1'

Use && or AND.

This will eliminate mistakes in your programmer future.

  • While there is some valid advice here (comparing proper types, expression consistency), this doesn't actually solve the problem of the faulty code. – El_Vanja Nov 25 '20 at 14:12