number/number.php

59 lines
1.5 KiB
PHP

<?php
$type = $_GET["type"];
$number = $_GET["number"];
if ($type=="hex") {
$number = base_convert($number,16,10);
$number_hex = base_convert($number,10,16);
} else {
$number_hex = base_convert($number,10,16);
}
if (array_key_exists("format",$_GET)) { // client wants a custom response
if ($_GET["format"]=="json") { // JSON response
header("Content-Type: application/json; UTF-8"); // we're responding with JSON
echo json_encode(array(
"decimal" => $number+0, // do funky shit so number is always of type `int`
"hex" => $number_hex,
"error" => NULL // no error here! (this is where we might later throw errors for invalid number types)
));
die(); // don't immediately follow this with the HTML page
}
// "We don't do that here."
http_response_code(400);
die("Bad request: format not in 'json'");
}
?>
<html>
<head>
<title>Number - info for numbers.</title>
<link rel="stylesheet" href="https://tilde.team/css/hacker.css">
</head>
<body><div class="container">
<h1>Number</h1>
<h3><?php
if ($type=="hex") {
echo "0x".$number_hex." is decimal ".$number;
} else {
echo "Decimal ".$number." is 0x".$number_hex;
}
?></h3>
<table class="table table-striped">
<thead>
<tr>
<th>System</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Decimal</td>
<td><?=$number?></td>
</tr>
<tr>
<td>Hexadecimal</td>
<td><?="0x".$number_hex?></td>
</tr>
</tbody>
</table>
</div></body>
</html>