I'm using this function to check if binary is correct, I know it looks sloppy.. I'm not sure how to write the function that well.. but it doesn't seem to work!
If binary = 10001000 it says malformed, even though it's not.. what is wrong in my function?..
function checkbinary($bin) {
$binary = $bin;
if(!strlen($binary) % 8 == 0){
return 1;
}
if (strlen($binary) > 100) {
return 1;
}
if (!preg_match('#^[01]+$#', $binary)){ //Tried without !
return 1;
}
if (!is_numeric($binary)) {
return 1;
}
}
if (checkbinary("10001000") != 1) {
echo "Correct";
} else {
echo "Binary incorrect";
}
Why does this function always say 10001000 is incorrect?
returnvery well, thestrlen($binary) > 100check makes me awfully suspicious he's trying to do a completely different thing.errorin the "original" implementation (see test case). Whether those tests make sense or not is hard or virtually impossible to decide for us (given the small amount of information we have). My only complain about it right now is that the function does more than the name implies (...and maybe the strange choice of return values =))