-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate.php
46 lines (42 loc) · 1.57 KB
/
calculate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/**
* Checks if imput contains numeric value
* @param string $variable value passed from form's input
* @return string $variable | boolean false Modified value received from form's input or false
*/
function check($variable)
{
$variable=trim($variable);
$variable=stripslashes($variable);
$variable=htmlspecialchars($variable);
if(is_numeric($variable))
return $variable;
else return false;
}
/*
CHANGE BELOW INTO FUNCTION(S)
@return string Returns prices per unit
*/
function calculatePrices($p1,$p2,$q1,$q2)
{
$price1=check($_GET["price1"]);
$price2=check($_GET["price2"]);
$quantity1=check($_GET["quantity1"]);
$quantity2=check($_GET["quantity2"]);
if($price1!=null && $price2!=null && $quantity1!=null && $quantity2!=null){
$pricePerUnit1=$price1/$quantity1;
$pricePerUnit2=$price2/$quantity2;
$better=($pricePerUnit1<$pricePerUnit2)?"1st":"2nd";
$output = "1st price per unit: ".number_format($pricePerUnit1,2)."<br>2nd price per unit: ".number_format($pricePerUnit2,2)."<br><p>".$better." offer is better</p>";
return $output;
}
$price1=check($_GET["price1"]);
$price2=check($_GET["price2"]);
$quantity1=check($_GET["quantity1"]);
$quantity2=check($_GET["quantity2"]);
if($price1!=null && $price2!=null && $quantity1!=null && $quantity2!=null){
$pricePerUnit1=$price1/$quantity1;
$pricePerUnit2=$price2/$quantity2;
$better=($pricePerUnit1<$pricePerUnit2)?"1st":"2nd";
echo "1st price per unit: ".number_format($pricePerUnit1,2)."<br>2nd price per unit: ".number_format($pricePerUnit2,2)."<br><p>".$better." offer is better</p>";
}