I'm trying to check if a variable has more than one zero. It seems php treats multiple zeros as one zero. For example, the following code always returns true no matter how many zeros the variable has:
$input = 0000;//or "0000"
if($input==00) echo "true";
else echo "false";
My question is : How can I make the above code return true only if it has the exact number of zero in the if statement?
Thanks
Currently, your $input
is a decimal integer. Therefore, 0
does in fact equal 00000
.
You need to define it as a string and then compare with other strings.
$input = "0000";
if( $input === "00" ) { echo "yes"; } else { echo "no"; }
How can I make the above code return true only if it has the exact number of zero in the if statement?
Use strings.
Integers is the only data of the number, not its presentation. So 0000
equals to 0
.
Use strings instead of numbers
$input = "00000";
// This searches for 0 extra zeros on the left
if(strpos($input,"0")==0 && $input!=="0"){
echo "true";
}
else{
echo "false";
}
<?php
$input = "0000";
if(strcmp($input,"0000")==0) {echo "they match";}
else {echo "They dont";}
?>