/
arithmetic.php
72 lines (66 loc) · 1.23 KB
/
arithmetic.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
function add($a, $b)
{
if (is_numeric($a) && is_numeric($b)) {
return $a + $b;
} else {
throwErrorMessage($a,$b);
}
}
function subtract($a, $b)
{
if (is_numeric($a) && is_numeric($b)) {
return $a - $b;
} else {
throwErrorMessage($a,$b);
}
}
function multiply($a, $b)
{
if (is_numeric($a) && is_numeric($b)) {
return $a * $b;
} else {
throwErrorMessage($a,$b);
}
}
function divide($a, $b)
{
if (is_numeric($a) && is_numeric($b) && $b != 0) {
return $a/$b;
} else {
throwErrorMessage($a,$b,true);
}
}
function modulus($a, $b) {
if (is_numeric($a) && is_numeric($b) && $b != 0) {
return $a%$b;
} else {
throwErrorMessage($a,$b,true);
}
}
function throwErrorMessage($a, $b, $zero = false) {
if ($zero) {
echo "{$a} and {$b} must be numeric and the second number can not be 0.\n";
} else {
echo "{$a} and {$b} must be numeric.\n";
}
}
// Add code to test your functions here
$a = 10;
$b = 5;
echo "Addition: ";
echo add($a,$b);
echo PHP_EOL;
echo "Subtraction: ";
echo subtract($a,$b);
echo PHP_EOL;
echo "Multiplication: ";
echo multiply($a,$b);
echo PHP_EOL;
echo "Division: ";
echo divide($a,$b);
echo PHP_EOL;
echo "Modulus: ";
echo modulus($a,$b);
echo PHP_EOL;
?>