-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
36 lines (35 loc) · 969 Bytes
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Binary Sum</title>
</head>
<body>
<h1>Binary Sum: Input only binary 1 or 0</h1>
<form method="post">
<label for="binaryA">Binary A:</label>
<input type="text" name="binaryA" id="binaryA" required pattern="[01]+">
<br>
<label for="binaryB">Binary B:</label>
<input type="text" name="binaryB" id="binaryB" required pattern="[01]+">
<br>
<input type="submit" value="Calculate Sum">
</form>
<h2>Result:</h2>
<?php
function binarySum($a, $b)
{
$decimalA = bindec($a);
$decimalB = bindec($b);
$sum = $decimalA + $decimalB;
$binarySum = decbin($sum);
return $binarySum;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$binaryA = $_POST["binaryA"];
$binaryB = $_POST["binaryB"];
$result = binarySum($binaryA, $binaryB);
echo "<p>{$result}</p>";
}
?>
</body>
</html>