PHP Classes

Profit and Loss PHP Calculator: Math class that can calculate profit and loss

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 64 All time: 10,406 This week: 89Up
Version License PHP version Categories
profitandlosscalcula 1.0Free For Educatio...5PHP 5, Finances, Math, Global
Description 

Author

This package provides a simple math class that can calculate profit and loss.

It provides a class that can perform addition and subtraction of amount values.

The package comes with an example script that presents a form for the user to enter the cost price and selling prices, and then it tells the user if there was a profit or loss.

Another example script can calculate the cost prices if it is missing from the selling price and the profit and loss percentages or can calculate the selling price if it is missing from the cost prices and the profit and loss percentages.

Innovation Award
PHP Programming Innovation award nominee
January 2023
Number 9
Finance is essential to measure the success or failure of a business in terms of whether it is earning or losing money.

The package can help calculate the cost and selling prices of products or services for profit or loss percentages.

Manuel Lemos
Picture of Dev Sharma
  Performance   Level  
Name: Dev Sharma <contact>
Classes: 4 packages by
Country: India India
Age: 22
All time rank: 3219210 in India India
Week rank: 227 Up15 in India India Up
Innovation award
Innovation award
Nominee: 2x

Example

<html>
<title>Profit and Loss - Simple calculator</title>
<head>
<script>
function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}
//for allowing only integers in our forms.
</script>
</head>
<body>
<form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<fieldset>
<tr>
<label for="CP">Cost Price</label></div></td>
<td><input name="CP" type="text" class="input" size="25" onkeypress="return isNumberKey(event)" />
</tr>
<tr>
<label for="SP">Selling Price</label></div></td>
<td><input name="SP" type="text" class="input" size="25" onkeypress="return isNumberKey(event)" />
</tr>
<tr>
<input type="submit" name="submit" value="Calculate!" />
</tr>
</fieldset>
</form>
</html>
<?php
require_once 'class.calc.php';

$calc = new Calc();

if(isset(
$_POST['submit'])){
$numOne = $_POST['CP'];
$numTwo = $_POST['SP'];

if(empty(
$numOne) or empty($numTwo)){
die(
'Field cannot be empty');
}

if (
$numOne === $numTwo) {
echo
'Therefore, as your selling price and the cost price is the same; you have earned no profit nor loss.';
echo
"<br>";
echo
'Profit' . ' = ' . $calc->sub($numOne, $numTwo) . "\n"; // we will now minus the inputs as per the formula
echo 'Loss' . ' = ' . $calc->sub($numTwo, $numOne) . "\n"; // we will now minus the inputs as per the formula
}

if(
$numOne > $numTwo) {
echo
'Therefore, as your cost price is higher than the selling price; there is a loss.';
echo
"<br>";
echo
' Loss ' . ' = ' . "\n" . $calc->sub($numOne, $numTwo); // we will now minus the inputs as per the formula
$losspercent = $calc->sub($numOne, $numTwo);
echo
' Loss Percentage ' . ' = ' . round(($losspercent / $numOne) * 100, 2) . '%'; //we will now find out the percentage; loss/total cost * 100 as per the formula and round up any decimal numbers to 2 decimal places.
}

if(
$numTwo > $numOne) {
echo
'Therefore, as your selling price is higher than the cost price; there is a profit.';
echo
"<br>";
echo
'Profit' . ' = ' . "\n" . $calc->sub($numTwo, $numOne); // we will now minus the inputs as per the formula
$profitpercent = $calc->sub($numTwo, $numOne);
echo
' Profit Percentage ' . ' = ' . round(($profitpercent / $numOne) * 100, 2) . '%'; //we will now find out the percentage; profit/total cost * 100 as per the formula and round up any decimal numbers 2 decimal places.
}

}
?>


Details

ProfitandLossCalculator

Profit and Loss calculator is a simple calculator which helps you find the profit or loss using the cost price and the selling price with percentage which rounds up-to 2 decimal places if needed. The calculator only follows the formula it is meant to follow in the profit and loss formulas.

AdvancedVersion

The advanced version helps you to find out the cost price or selling price(if it's missing from your problem sum) with the help of profit or your loss given below in your problem sum.

Enjoy!


  Files folder image Files (4)  
File Role Description
Accessible without login Plain text file AdvancedPnL.php Aux. Auxiliary script
Plain text file class.calc.php Class Class source
Accessible without login Plain text file PnL.php Example Example script
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:64
This week:0
All time:10,406
This week:89Up