skip to Main Content

I am running this code to match strings for simple value determine and return.

I have been unable to see a issue with this code

Can someone tell me why this code comes up empty there should be a value retured to the ring_size var any help greatly appreacited!!


$order_item = '5.5';

if (!$ring_size && isset($order_item)) {
        $ring_size = matchRingSize($order_item);
        echo "ring size match";
        echo $ring_size;
        } else {

            echo "no ring size match";
            echo $ring_size;

    }


function matchRingSize($str) {
    if (strpos($str, '3.5') !== false) {
        return "5";
    } elseif (strpos($str, '4') !== false) {
        return "5";
    } elseif (strpos($str, '4.5') !== false) {
        return "5";
    } elseif (strpos($str, '5') !== false) {
        return "5";
    } elseif (strpos($str, '5.5') !== false) {
        return "5";
    } elseif (strpos($str, '6') !== false) {
        return "7";
    } elseif (strpos($str, '6.5') !== false) {
        return "7";
    } elseif (strpos($str, '7') !== false) {
        return "7";
    } elseif (strpos($str, '7.5') !== false) {
        return "7";
    } elseif (strpos($str, '8') !== false) {
        return "7";
    } elseif (strpos($str, '8.5') !== false) {
        return "7";
    } elseif (strpos($str, '9') !== false) {
        return "7";
    } elseif (strpos($str, '9.5') !== false) {
        return "7";
    } elseif (strpos($str, '10') !== false) {
        return "7";
    } elseif (strpos($str, '10.5') !== false) {
        return "7";
    } elseif (strpos($str, '11') !== false) {
        return "7";
    } else {
        return $str;
    }
}

2

Answers


  1. Undefined variable $ring_size: If $ring_size is not initialized before this if block, you might get an "undefined variable" notice, as PHP would not know what $ring_size is. You could avoid this by initializing $ring_size to null or some default value before the if block.

    Below is your code with some modifications:

    $order_item = '5.5';
    $ring_size = null; // Initialize ring_size to avoid undefined variable notice
    
    if (!$ring_size && isset($order_item)) {
       $ring_size = matchRingSize($order_item);
       echo "Ring size match: ";
       echo $ring_size;
    } else {
       echo "No ring size match: ";
       echo $ring_size;
    }
    
    function matchRingSize($str) {
       if (strpos($str, '3.5') !== false) {
          return "5";
       } elseif (strpos($str, '4') !== false) {
          return "5";
       } elseif (strpos($str, '4.5') !== false) {
          return "5";
       } elseif (strpos($str, '5') !== false) {
          return "5";
       } elseif (strpos($str, '5.5') !== false) {
          return "5";
       } elseif (strpos($str, '6') !== false) {
          return "7";
       } elseif (strpos($str, '6.5') !== false) {
          return "7";
       } elseif (strpos($str, '7') !== false) {
          return "7";
       } elseif (strpos($str, '7.5') !== false) {
          return "7";
       } elseif (strpos($str, '8') !== false) {
          return "7";
      } elseif (strpos($str, '8.5') !== false) {
          return "7";
      } elseif (strpos($str, '9') !== false) {
          return "7";
      } elseif (strpos($str, '9.5') !== false) {
          return "7";
      } elseif (strpos($str, '10') !== false) {
          return "7";
      } elseif (strpos($str, '10.5') !== false) {
          return "7";
      } elseif (strpos($str, '11') !== false) {
          return "7";
      } else {
          return $str;
      }
    }
    
    Login or Signup to reply.
  2. You’re looking to do a number processing script that converts an input size to several ranges of output sizes, it runs just fine on my end, but the code problem needs to be addressed are input data types, server-side security (filtering), and conversion logic.

    The actual problem might be that, you simply tried to open it directly in a browser, without an HTTP server that handles your PHP script.

    I’ve used XAMPP and replaced $order_item = '5.5' with $order_item = isset($_GET['order_item']) ? $_GET['order_item'] : null;. Here is the HTML file that calles the PHP script under the same directory:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Ring Size Matcher</title>
        <script>
            // JavaScript function to call the PHP script via AJAX
            function checkRingSize() {
                var orderItem = document.getElementById("order_item").value;
                
                // Create a new AJAX request to fetch the ring size
                var request = new XMLHttpRequest();
                request.open("GET", "ring_size_match.php?order_item=" + encodeURIComponent(orderItem), true);
                request.onreadystatechange = function() {
                    if (request.readyState == 4 && request.status == 200) {
                        // Display the response in the result div
                        document.getElementById("result").innerHTML = request.responseText;
                    }
                };
                request.send();
            }
        </script>
    </head>
    <body>
        <h1>Check Ring Size</h1>
        
        <!-- Input field for the order item -->
        <label for="order_item">Enter Order Item (e.g., 5.5):</label>
        <input type="text" id="order_item" name="order_item" onChange="checkRingSize()" />
        
        <!-- Placeholder for the output result -->
        <div id="result"></div>
    </body>
    </html>
    

    Here are a detailed list of problems can be fixed:

    1. Mis-use of strpos in matchRingSize($str)
    • input "-14" would result an output of 5, because strpos($str, '4') > false
    • PHP is a weakly-typed langauge, so false and true are basically 0 and 1
    1. $ring_size is not initialized, only $order_item is initialized

    2. Double echo

    • This would overwrite your first echo statement, so you may not be able to see anything

    Here is the fixed PHP code:

    <?php
    // Get the order item from the GET request with server-side security compliance
    $order_item = filter_input(INPUT_GET, 'order_item', FILTER_VALIDATE_FLOAT);
    
    // If the float validation failed, check for integer validation
    if ($order_item === false) {
        $order_item = filter_input(INPUT_GET, 'order_item', FILTER_VALIDATE_INT);
    }
    
    // If neither float nor int is valid, return with error
    if ($order_item === false) {
        echo "Invalid input. Please input a valid numeric value.";
        exit;  // Stop further execution
    }
    
    /**
     * Convert input sizes to ring sizes, not sure what kind of ring is it lol
     * @param float|int $order_item Input size for ring size detection
     * @return string|null Approariate ring size
     */
    function matchRingSize(int|float $order_item) {
        // Valid sizes to fit the ring
        $size_xs_max = 3.5;
        $size_s_max = 5;
        // $size_m_max = 7;
        // $size_l_max = 9;
        $size_xl_max = 11;
    
        // Process valid sizes
        if (($order_item >= $size_xs_max) && ($order_item <= $size_s_max)) {
            return "5";
        }
        else if (($order_item > $size_s_max) && ($order_item <= $size_xl_max)) {
            return "7";
        }
        /**
         * The else-if above simplfies:
         * 
         * else if (($order_item >= $size_s_max) && ($order_item <= $size_m_max)) {
         *     return "7";
         * }
         * else if (($order_item > $size_m_max) && ($order_item <= $size_l_max)) {
         *     return "7";
         * }
         * else if (($order_item > $size_l_max) && ($order_item <= $size_xl_max)) {
         *     return "7";
         * }
         */
        else { return null; }
    }
    
    // Valid condition return
    $ring_size = matchRingSize($order_item);
    if ($ring_size !== null) {
        echo "Ring size matched: " . $ring_size;
    }
    else {
        echo "No ring size matched for: " . htmlspecialchars($order_item);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search