Are you looking to make your own PHP Percentage Calculator?, then you are at the right place.
What you will learn from this PHP script?
1. You will know how to divide a number in PHP.
2. You will learn how to multiply a number in PHP.
3. You will learn how to check a form is submitted in PHP.
What is the formula to find percentage of a number?
Formula:
a% x b = c
Re-written the above formula for PHP:
Remove the % and divide by 100, then instead of x add *
c = (a / 100) * b
a – percentage value
b – number
c- answer
Here is the entire PHP script with the comments in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php //check if the form is submmited if($_POST){ //get the input values $percent = $_POST['percent']; $number = $_POST['number']; // slash(/) symbol is for dividing values, star * symbol for multiplication $answer = ($percent / 100) * $number; } ?> <html> <head> <title>PHP Percentage Calculator script</title> </head> <body> <h2>PHP Percentage Calculate Tutorial - by <a href="http://www.tutorialsmade.com/php-percentage-calculator-script/">Tutorialsmade.com</a></h2> <!-- action is blank, which means it will submit in the same page --> <form action="" method="post"> <!-- isset() is used to check if the variable is set or not--> <input size="7" type="text" name="percent" value="<?php echo isset($percent) ? $percent : '';?>" /> % of <input size="10" type="text" name="number" value="<?php echo isset($number) ? $number : '';?>" /> = <input size="14" type="text" name="a-ans" value="<?php echo isset($answer) ? $answer: ''; ?>" class="disabled-cursor" disabled /> <input value="Calculate" type="submit" /> </form> </body> </html> |
Read the comments in the above script to understand how the script works.
Here is the demo to see how the script works:
It’s amazing to pay a visit this website and reading the views of all mates concerning this article, while I
am also keen of getting knowledge.
nice very nice it’s very helpful
Hi Agurchand, please can you help me modify this script to read values from database, calculate 1% of each of the values and store it in another table in the database. Am currently coding a daily profit calculation script for users, i expect to execute it using cron.
You can enter the marks in the total subject field you want and edit the full marks accordingly, then calculate the percentage of the total marks.