Checking multiple zero - php

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

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.

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"; }

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";}
?>

Related

How many = signs are required to equal a value in php?

I have following code which is not working with three equal signs (===). Hence it is working with single equal sign (=). I always use === in such cases and that works fine. I am confused why it is not working in this case?
$vendor_name = "Stock Returned";
if($vendor_name = "Stock Returned")
{
$stock_return === "Yes";
}
else
{
$stock_return = "No";
}
echo $stock_return;
Result of above code is showing "No". But in my understanding it should be "Yes"
The 1 equal sign (=) is used for assigning values, it is not used for comparisons.
Comparisons are made with == or ===, that makes your $stock_return === "Yes"; line incorrect as well because it looks like you want to assign a value to $stock_return
The difference between == and === is that
== is for testing if both values are equal, with php, a string '20' and an integer 20 are equal
=== is more strict and will test the type as well so a string '20' is no longer equal to an integer 20
Your code should look something like this to be correct
$vendor_name = "Stock Returned";
if($vendor_name == "Stock Returned")
{
$stock_return = "Yes";
}
else
{
$stock_return = "No";
}
echo $stock_return;

PHP Check for special numbers

I have a simple function which checks if a value is a number and if that number is less than 0.
function check_number($value){
if(!is_numeric(strval($value))){
return "value must be whole or decimal";
} else {
if($value < 0){
return "value must be bigger than 0";
}
}
return "successful value";
}
This functions works all well and good, until special numbers are passed in such as:
0xf4c3b00c
1e7
These values will still make the function return "successful value".
How can I make these special numbers not return a false positive in my case.
Thanks.
function check_number($value){
if (!is_numeric($value)) {
return "That is not a number";
}
if ($value < 0) {
return "value must be bigger than 0";
}
if(!ctype_digit(str_replace('.', '', $value))){
return "value must be whole or decimal";
}
return "successful value";
}
SEE DEMO
You have peculiar requirements. Your definition of a number differs from PHP's - 4.1e7 is a number, just not one formatted in a way you like. So you need to look at the number as it appears - as a string.
Check if value is less than 0 first, because later we won't distinguish between a minus sign and any other character.
Count dots in the number string.
If more than 1 dot, return fail.
If just 1 dot, str_replace it out and continue.
Finally, use ctype_digit on the remaining string.
Change
if(!is_numeric(strval($value))){
into
$value = trim($value, "0"); // get value as string and remove leading and trailing zeroes
$value_float = ltrim(floatval($value), "0"); // get float value as string and remove leading zero
if($value !== $value_float){
or to make it compacter
if(trim($value, "0") !== ltrim(floatval($value), "0")){
to check whether the numerical representation is still the same after stringifying.

PHP - if condition match exact value including decimal

i am trying to match exact number with decimal. i have tried following but doesn't work
tried following code, but doesn't work with decimal.
<?php
$number=1.23;
$numbers=1.28;
if (is_float($number)==is_float($numbers))
{
echo 'matched';
}else{
echo 'not matched';
}
?>
please check where i am doing mistake or it's totally wrong way to do that. i have check above in PHP sites.
Use floatval (which returns the float value of the given variable) instead of is_float (which returns true if the given variable is a float and false if it isn’t).
You could simply make use of a strict matching operator === instead of floatval or is_float.
<?php
$number=1.23;
$numbers=1.28;
if($number === $numbers)
{
echo "Matched";
}
else { echo "No Match"; }

Int or not Int. How to fix the code?

I would like the code below to return 'Int' the first time and 'Not int' the second time. Unfortunately it returns 'Not int' twice instead.
How can I fix this?
<?php
$test1='1';
if(is_int($test1)){
echo "Int";
}else{
echo "Not int";
}
echo "\n";
$test2='1a';
if(is_int($test2)){
echo "Int";
}else{
echo "Not int";
}
?>
By wrapping the number in quotation marks '1', you are declaring a string.
Instead you got to use $test1 = 1;.
By using the PHP ctype_digit() function, you can check if a string only contains digits.
You could also use the is_numeric() function, which also returns true if the string contains a exponential part like +0123.45e6 or a hex value 0xFF.
is_int - Find whether the type of a variable is integer
Because you put the number in quotes, it is a string. Therefore is_int = false
is_numeric — Finds whether a variable is a number or a numeric string
Because the string is actually a number, is_numeric will return true
So, change is_int to is_numeric and it works:
<?php
$test1 = '1';
if (is_numeric($test1))
{
echo 'Int';
}
else
{
echo 'Not int';
}
echo "\n";
$test2 = '1a';
if (is_numeric($test2))
{
echo 'Int';
}
else
{
echo 'Not int';
}
?>
Use ctype_digit() instead.
ctype_digit('1'); // True
ctype_digit('1a'); // False
change
$test1='1';
to
$test1=1;

How can one check in PHP if a string can be converted into a whole number?

For instance, "000", "404", and "0523" can be converted into whole numbers in PHP, but "42sW" and "423 2343" cannot be converted into whole numbers.
Use the ctype_digit function. is_numeric will also permit floating point values.
$numArray = array("1.23","156", "143", "1w");
foreach($numArray as $num)
{
if (ctype_digit($num)) {
// Your Convert logic
} else {
// Do not convert print error message
}
}
}
PHP's is_numeric() can determine if a given param is a number or a number string. Have a read through the manual for some examples.
ctype_digit should be what you're looking for.
42Sw can be converted to a number by using intval()
echo intval("42sW"); // prints 42
use is_numeric():
if (is_numeric("string")) {
echo "This can be converted to a number";
}
You could try something like this.
<?php
if (is_numeric($string)) {
//functions here
}
else{
//functions2 here
}
?>
$test = "42sW";
if (ctype_digit($test)) {
echo "The string $test consists of all digits.\n";
} else {
echo "The string $test does not consist of all digits.\n";
}
//OR
is_numeric($test); // false

Categories