forked from assembler-institute/php-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
43 lines (34 loc) · 916 Bytes
/
functions.php
File metadata and controls
43 lines (34 loc) · 916 Bytes
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
<?php
// Create a function that given two numbers returns the sum of both
function sum($a, $b) {
return $a + $b;
}
echo sum(1, 2);
echo "<br>";
// Create a function that given two numbers returns the multiplication of both
function multiply($a, $b) {
return $a * $b;
}
echo multiply(1, 2);
echo "<br>";
// Create a function that given two numbers returns the division of both
function divide($a, $b) {
return $a / $b;
}
echo divide(1, 2);
echo "<br>";
// Create a function that, given two numbers and an operation (add, multiply or divide), returns the result of that operation.
function operate($a, $b, $operation) {
if($operation == "+") {
return sum($a, $b);
} else if ($operation == "*") {
return multiply($a, $b);
} else if ($operation == "/") {
return divide($a, $b);
} else {
return "error";
}
}
echo operate(3, 2, "*");
echo "<br>";
?>