-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.php
More file actions
43 lines (33 loc) · 1.02 KB
/
math.php
File metadata and controls
43 lines (33 loc) · 1.02 KB
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
<html>
<head>
<title>Simple Calculator</title>
<style type="text/css" media="screen"> .number { font-weight: bold; }
</style>
</head>
<body>
<?php
// ch04 Homework - math.php
// Retrieve the numbers, mathematical operation and number of decimal
// places to display from the _POST array
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$operation = $_POST['operation'];
$decpoints = $_POST['decpoints'];
$expression = "$num1 $operation $num2";
// Evaluate the mathematical expression and display the result
$result = eval("return($expression);");
// Evaluate the result of the expression to the specified number of
// decimal points.
//
// If the integer portion of the result is equal to the result, remove
// the decimal points from the result.
$result = number_format($result, $decpoints);
$int_result = floor($result);
if ($result == $int_result) {
$result = $int_result;
}
print "<p>Result:<br />
<span class=\"number\">$expression = $result</span></p>";
?>
</body>
</html>