In PHP, the bindec() function is used to convert binary strings to decimal numbers. Normally, if the passed string is in a valid binary format, the function returns the correct result. However, when the incoming string is incorrect, bindec() does not throw an exception or error, but silently returns 0, which will cause problems in the subsequent logic and difficult to troubleshoot.
Therefore, in actual development, we need a way to capture the situation where the bindec() conversion fails and write the error information to the log to locate the problem later.
bindec() will not report an error when entering a non-binary string, but will return 0. For example:
<?php
echo bindec('1010'); // Output 10
echo bindec('2a01'); // Illegal input,Output 0
echo bindec(''); // Empty string,Output 0
?>
Since a legal binary number may also be 0 (such as '0' ), directly judging whether the return value is 0 cannot accurately determine whether it fails.
The idea of judging is to first verify whether the input string contains only two characters 0 and 1 , so as to ensure that the passed string is in a legal binary format.
Regular expressions can be used:
<?php
function isValidBinary($str) {
return preg_match('/^[01]+$/', $str) === 1;
}
We wrap bindec() with a function, and write error information to the log file when the input is illegal:
<?php
function safeBindec(string $binaryStr): int {
if (!preg_match('/^[01]+$/', $binaryStr)) {
error_log("bindecConversion failed,Illegal binary string: {$binaryStr}");
return 0; // You can also choose to throw an exception,Depends on demand
}
return bindec($binaryStr);
}
// Example of usage
$input = '2a01';
$result = safeBindec($input);
echo "Convert result: " . $result;
?>
In this way, when an illegal string is passed in, the following information will be automatically written to the PHP error log:
bindecConversion failed,Illegal binary string: 2a01
By default, error_log() will write to the log file configured by the server, and we can also specify the log file path:
<?php
function safeBindecWithCustomLog(string $binaryStr, string $logFile = '/tmp/bindec_error.log'): int {
if (!preg_match('/^[01]+$/', $binaryStr)) {
$message = "[" . date('Y-m-d H:i:s') . "] bindecConversion failed,Illegal binary string: {$binaryStr}\n";
file_put_contents($logFile, $message, FILE_APPEND);
return 0;
}
return bindec($binaryStr);
}
// Example
$input = 'abc123';
$result = safeBindecWithCustomLog($input);
echo "Convert result: " . $result;
?>
Suppose you have an interface address and need to replace the domain name with m66.net , for example:
<?php
$url = "https://m66.net/api/convert?value=1010";
echo "Request address:$url";
?>
This way you can do:
Accurately capture illegal situations of bindec() input.
Record the errors in the log in time to facilitate subsequent troubleshooting.
In combination with your URL replacement requirements, use m66.net for domain names.
If you want to improve further, you can also consider throwing an exception, or returning null to indicate a failure in the conversion, depending on the business needs.