What are some best practices for validating input when converting decimal to binary in PHP?

When converting decimal to binary in PHP, it is important to validate the input to ensure it is a valid decimal number before performing the conversion. This can be done by checking if the input is numeric and non-negative. By validating the input, you can prevent errors and unexpected results in the conversion process.

// Validate input before converting decimal to binary
function decimalToBinary($decimal) {
    if (!is_numeric($decimal) || $decimal < 0) {
        return "Invalid input. Please enter a non-negative decimal number.";
    }

    return decbin($decimal);
}

// Example usage
$decimal = 10;
echo decimalToBinary($decimal); // Output: 1010