1. ## percentage

Is there a function for PHP 3 / 4 that I can use that will get the percentage from two numbers? I have a quiz program, and say I got 4 (\$yourscore) out of 5 (\$board_config['questions_quiz']), what could I do to get a %? I have tried dividing then multiplying by 10, but it doesn't work at all like it should

Thanks

2. There is no function given, but this should work:
Code:
```function percentage(\$a, \$b)
{
return (\$b / (\$a / 100));
}

echo percentage(4, 5); // 80```

3. What is \$a and \$b assigned to? And in the echo line do I replace 4, 5 with \$a, \$b

Thanks for the code and help

4. To find the percentage score...
PHP Code:
``` \$score = \$total_possible/100*\$quiz_result;  ```
where \$total_possible is the total score possible and \$quiz_result is the players result.

5. Thanks!! It worked!

6. But that has nothing to do with PHP itself, it's just maths..

60% is just the same as 0.6

5.4% is 0.054

To get 6.7% of a total of 775: 775 * 0.067

Or to add 5% to a total of 120: 120 x 1.05

.....

7. It may be basic maths, but it does what he needed. Just cos it ain't php don't mean it ain't right!

8. I knew the maths, but it turned out I was using the wrong symbol. I thought x was multiply when it was actually * in PHP

9. Originally Posted by battye
I knew the maths, but it turned out I was using the wrong symbol. I thought x was multiply when it was actually * in PHP
Haha... Don't you hate those stupid errors? Glad you got it working.

10. Yeah haha, thanks

What if I have a form where someone enters a percentage rate[tax] in the form of
Code:
`Please enter tax: 5.9 [%]`
and I need to get the total of all items plus the tax. How do I tell php that the number 5.9 is actually a percent (0.059) so that the math is done correctly?

12. Use the form text entry as you would for posting variables....
PHP Code:
``` \$percentage_entered = \$_POST['percentage_entered']; \$tax = \$total_amount/100*\$percentage_entered;  ```
Make sense?

13. I feel very stupid. Didn't think of doing it that way. Sooo simple.

Thanks!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•