-1

I am a newbie learning PHP and i am trying to find average of 3 numbers but not getting the correct answer. I don't know where i am going wrong.

function percentage($math,$eng,$sc){ $s = $math+$eng+$sc / 3 ; return $s; } $p = percentage(10,20,30); echo $p; 

I am getting the ansewer as 40 whereas i am supposed to get 20. Kindly check if there is any error.

4
  • Hint: $s = ($math + $eng + $sc) / 3;. Commented Jun 2, 2021 at 6:59
  • 1
    you missed bracket Commented Jun 2, 2021 at 7:00
  • 1
    why do you call your function percentage() and not average()? Commented Jun 2, 2021 at 7:03
  • Thank you for your answer. I really appreciate..@zohorov & @Nikita for prompt response. Commented Jun 2, 2021 at 7:04

2 Answers 2

0

Return value is right. Check operators precedence.

If you want 20 as return value code is:

$s = ($math+$eng+$sc) / 3 ; 
Sign up to request clarification or add additional context in comments.

Comments

0

You forgot to use parentheses:

$s = ($math+$eng+$sc) / 3 ; 

All things together:

function percentage($math,$eng,$sc){ $s = ($math+$eng+$sc) / 3 ; return $s; } echo percentage(10,20,30); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.