Home > Software design >  PHP | Number conversion
PHP | Number conversion

Time:04-11

how to do 'number conversion' in PHP using input 'radio'? I have ready form.

<form action="test.php" method="post" >
<input type="text" name="value" placeholder="Enter value">
<br>
<h1>Select Option </h1>
<input type="radio" name="bin" value="b">BIN
<br>
<input type="radio" name="hex" value="h">HEX
<br>
<input type="radio" name="oct" value="o">OCT
<br>
<input type="submit"  value="Show Result">

CodePudding user response:

Firstly, you have to fix your form by setting up a common name for all your radio buttons.

<input type="radio" name="system" value="bin">BIN</input>
<br>
<input type="radio" name="system" value="hex">HEX</input>
<br>
<input type="radio" name="system" value="oct">OCT</input>

Now, once you submit your form, you will be able to get the selected system using $_POST['system'].

To do the conversion, the PHP has built-in functions using the pattern "orig-system dest-system", like:

decbin(number) // to binary
decoct(number) // to octal
dechex(number) // to hexadecimal

CodePudding user response:

PHP provides some built-in functions that may help you:

decbin : converts a base 10 number to its base 2 (binary) representation.

decoct : converts a base 10 number to its base 8 (octal) representation.

dechex : converts a base 10 number to its base 16 (hexadecimal) representation.

Imagining you have the following form:

<form action="convert.php" method="post">
    <input type="number" name="value" placeholder="Enter value">
    <br>
    <h1>Select Base</h1>
    <!-- the radio buttons must have the same name but each one should have its own value -->
    <input type="radio" name="base" value="b">BIN
    <br>
    <input type="radio" name="base" value="h">HEX
    <br>
    <input type="radio" name="base" value="o">OCT
    <br>
    <button type="submit">Convert</button>
</form>  

When the user enters a number in the field named value and chooses a base from the base named options and considering your form uses the POST method, at the PHP level you'll have the values in the $_POST super-global as follows:

  • $_POST['value'] : holds the number entered by the user.

  • $_POST['base'] : the chosen base to convert to.

You can the write a function that returns the wanted representation of the entered value:

/** converts an integer into binary or octal or hexadecimal bnase */
function convert($num, $base) {
    // as the values set in the form for the "base" fields, only "b", "o" and "h" are the expected values to be received.
    $converters = [
        'b' => fn($n) => decbin($n),
        'o' => fn($n) => decoct($n),
        'h' => fn($n) => dechex($n)
    ];
    // convert and return
    return $converters[$base]($num);
}

And you may call that function, as follows, to get the resulting representation:

$representation = convert($_POST['value'], $_POST['base']);

In the end, the above examples should work well in a perfect world but we shouldn't trust user inputs at all so having a validation before doing the conversion is highly recommended.

  • Related