-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmetic.php
More file actions
83 lines (73 loc) · 2.01 KB
/
arithmetic.php
File metadata and controls
83 lines (73 loc) · 2.01 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
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
73
74
75
76
77
78
79
80
81
82
83
<?php
function add($a, $b)
{
// check to see if a and b are numeric. if so run the return code
if(isNumeric($a, $b)){
return $a + $b;
// otherwise run the errorfunction that returns the error message
} else {
return errorFunction($a, $b);
}
}
function subtract($a, $b)
{
// check to see if a and b are numeric. if so run the return code
if(isNumeric($a, $b)){
return $a - $b;
// otherwise run the errorfunction that returns the error message
} else {
return errorFunction($a, $b);
}
}
function multiply($a, $b)
{
// check to see if a and b are numeric. if so run the return code
if(isNumeric($a, $b)){
return $a * $b;
// otherwise run the errorfunction that returns the error message
} else {
return errorFunction($a, $b);
}
}
function divide($a, $b)
{
// if user is trying to divide by zero return error message
if($b == 0){
return "Error: Cant divide by 0.";
// otherwise continue to check if its numeric
} else {
// if its numeric do a/b
if(isNumeric($a, $b)){
return $a / $b;
// if provided values are not numeric then run errorFunction which spits
// out error message
} else {
return errorFunction($a, $b);
}
}
}
function modulas($a, $b)
{
// check to see if a and b are numeric. if so run the return code
if(isNumeric($a, $b)){
return $a % $b;
// otherwise run the errorfunction that returns the error message
} else {
return errorFunction($a, $b);
}
}
// function that checks to see if a and b are numeric.
function isNumeric($a, $b)
{
return is_numeric($a) || is_numeric($b);
}
// function returns the error message.
function errorFunction($a, $b)
{
return "ERROR: Arguments \$a and \$b must be numbers";
}
echo add('shits','broke') . PHP_EOL;
echo subtract(6,4) . PHP_EOL;
echo multiply(6,4) . PHP_EOL;
echo divide(6,0) . PHP_EOL;
echo modulas(6,4) . PHP_EOL;