Hello, I'm new into PHP. Just migrated from Java recently.
I'm trying to make a conversion machine that conver number to romans number.
Here is the code I wrote :

!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Latins to Romans Conversion</title>
</head>
<body>
    <h2>Latin to Romans Conversion</h2>

    <form method="post">
    Input Your Number : <input type="text" name="number"><br><br>
    <input type="submit" name="convert" value="submit">
    <input type="reset" value="reset"><br><br>

    <?php 

        if(isset($_POST['convert'])) {
            $number = $_POST['number'];

           function convertToRoman($number) {
               function getNum($digit, $lowStr, $midStr, $nextStr) {
                   switch (true) {
                       case $digit <= 3: return str_repeat($lowStr, $digit);
                       case $digit == 4: return $lowStr + $midStr;
                       case $digit <= 8: return $midStr + str_repeat($lowStr, $digit);
                       default: return $lowStr + $nextStr;
                   }
               }
               $str = "0";

               //Ribuan
               $str += str_repeat("M", floor($number / 1000)) . $number %= 1000;

               //Ratusan
               $str += getNum(floor($number / 100), 'C', 'D', 'M') . $number %= 100;

               //Puluhan
               $str += getNum(floor($number / 10), 'X','L', 'C') . $number %= 10;

               //Satuan
               $str += getNum($number, 'I', 'V', 'X');

               return $str;
           }
           $decimal = $number;
           echo "The Roman's form of this decimal number is", convertToRoman($decimal);
           return $decimal;
        }

    ?>
</body>
</html>

It display these errors:

Warning: A non-numeric value encountered in D:\xampp\htdocs\omniwrench\latinromans.php on line 39
Warning: A non-numeric value encountered in D:\xampp\htdocs\omniwrench\latinromans.php on line 42

Anyway, I'm still using PHP 7. I tried declare of each romans value in an array like this (just example) :

$mapHundreds = array('M' => 1000);

Anyone got solution? I've been searching the problem about this for an hour now. Thanks!

Recommended Answers

All 3 Replies

Hmm... It's weird how you've managed to arrange the code... First of all you have a function in a function. Second, I don't remember using the switch statement that way... The variables namings are all wrong (for example.... getNum() is not returning you a number but a string). I don't know what java you did before this. But anyway, a good idea is to not add unneecessary complexity where there id no need for it. Also, comment your code. I didn't understand anything from it.

$number is probably null or a srting.
Maybe check for missing value and something like is_int() or is_numeric() to check if it's a number.

if(!$number || !is_int($number)){
    $number = 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.