PHP - if condition match exact value including decimal - php

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

Related

Checking multiple zero

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

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;

Multiple PHP IF conditions not working

I am trying to make sure the GET string is set in the URL and that it's value is an integer, but I can't get this to work.
if (isset($_GET['allusers']) && is_int($_GET['allusers'])) {
echo "works";
}
Am I doing something wrong with my parentheses?
A $_GET variable can't be an integer. It'll always be a string.
To test is it's a numeric string, use is_numeric():
if ( isset($_GET['allusers']) && is_numeric($_GET['allusers']) )
{
echo "works";
}
In your code
isset($_GET['allusers'])
will be evaluated to be true but
is_int($_GET['allusers']) will not as the value of $_GET is a string not int you can modify your code as
if (isset($_GET['allusers']) && is_int(intval($_GET['allusers']))) {
echo "works";
}
This will work
Use ctype_digit if you are expecting only non-negative integers. This will give the best result in those cases since it allows only the numbers 0-9.
Note that is_numeric will return true for strings which can be converted to integers, both negatives and floats. A few examples of what is_numeric will consider to be true:
0xF5 (hexadecimal)
-.0e-000 (a strange way of expressing 0.0)
-0.4
is_int returns false on a string, which is what a GET variable will be.
var_dump(is_int("23"));
bool(false)
You should be using is_numeric instad.

Validating whether $_REQUEST contents is an int

I am trying to do a basic operation: to check whether string is a number.
This does not work:
$qty = $_REQUEST[qty];
if (is_int($qty) == FALSE) {
echo "error";
} else {
echo "ok";
}
This one does:
$qty = 1;
if (is_int($qty) == FALSE) {
echo "error";
} else {
echo "ok";
}
$_REQUEST[qty] is posted with AJAX request ($.post). $_REQUEST[qty] is NOT empty and contains only number (1).
is_numeric() is not going to work, since it treats 1e4 as a number.
is_int will only return true if the variable is of integer type.
if you are trying to test if the variable contains a string which represents a number,
use:
is_numeric("1");
source: http://php.net/manual/en/function.is-int.php
EDIT:
use ctype_digit() to check for every character in the string if it's a number to rule out "1e4"
If you want to check for the presence of digits only, you can use ctype_digit .
try "is_numeric()" instead of "is_int"...
i think that u r getting a String from your Request...
and is_int really checks wether a given object is a integer... But it isn't -> it's a String.
is_numeric just checks, wether a object is convertable into an integer. If so, it returns true, otherwise false...
$qty = $_REQUEST[qty];
if (is_numeric($qty) == FALSE) {
echo "error";
} else {
echo "ok";
}
PS: Use $_POST[] or $_GET[] insetead of $_REQUEST[] ;)
You mention you cannot use is_numeric because it treats 1e4 as a number. Well, 1e4 is a number. Specifically 1 * 10^4.
You could use is_int(intval($_REQUEST['qty'])), but as intval always returns an int (0 on failure or empty input) you run the risk of false positives. However, combined with is_numeric or filter_var you should be on pretty solid ground.
Actually, all $_REQUEST (as well as $_GET, $_POST, etc) values are always strings.
When $qty is $_REQUEST[qty] it's an string, not an integer. When $qty is 1, it's already an integer.
Use intval function to convert it to an integer. But as you say, you only want to find out whether it's an integer or not, so use floatval to convert it, then check if they are equal:
if (intval($qty) == floatval($qty)) {
echo "Ok!";
} else {
echo "error";
}
Do a not-identical comparison­Docs while using the string and integer cast:
if ($qty !== (string)(int)$qty) {
echo "error";
} else {
echo "ok";
}
This is basically literal: As all incoming variables are strings, you can't check them being an integer, unless you cast them to an integer and then back to string. Wrap it into a function if it's to hard to grasp in inline code what it does:
/**
* string is integer value?
*
* #return bool
*/
function is_int_string($string)
{
return $string === (string)(int)$string;
}
$qty = $_REQUEST[qty];
if (is_int_string($qty) == FALSE) {
echo "error";
} else {
echo "ok";
}
HTTP is a text protocol, so there is only string in the beginning.
I decided to go with preg_match().
if (!preg_match("/^[0-9]+$/", $_REQUEST[qty])) {}

Is this a bug in PHP

<?php
if(stripos('http://cp.uctorrent.com', 'cp.utorrent.com') >= 0){
echo "Good1";
}else{
echo "Bad1";
}
if(stripos('http://uctorrent.com', 'cp.utorrent.com') >= 0){
echo "Good2";
}else{
echo "Bad2";
}
?>
output is
Good1Good2
whereas it should be
Good1Bad2
<?php
if(false >= 0) echo "Good";
else echo "Bad";
// this code prints Good
?>
It's not a bug, it's a "weird" boolean conversion.
stripos returns false when the string is not found, and false converts to 0 in PHP.
Directly from the documentation (the problem is the other way around) :
Warning This function may return
Boolean FALSE, but may also return a
non-Boolean value which evaluates to
FALSE, such as 0 or "". Please read
the section on Booleans for more
information. Use the === operator for
testing the return value of this
function.
In case 2 stripos returns false as the search fails and false when compared with 0 returns true.
The right way of doing it is to use the identity operator which checks both type and value:
if(stripos('http://cp.uctorrent.com','cp.utorrent.com') !== false)
echo "Good1"; ^^^^^^^^^^
else
echo "Bad1";
Reading the manual would help greatly:
Warning
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
If needle is not found, stripos() will return boolean FALSE.
http://php.net/manual/en/function.stripos.php
Boolean FALSE in PHP is equivalent to integer 0, which is >= 0.
If you want to check whether stripos failed to get a match, you need to test for type and value with !== or ===, for example:
<?php
if(stripos('http://cp.uctorrent.com','cp.utorrent.com')!==false)echo "Good1";
else echo "Bad1";
if(stripos('http://uctorrent.com','cp.utorrent.com')!==false)echo "Good2";
else echo "Bad2";
?>
Again, try this and avoid the issues caused by microoptimization function use patterns:
if (stristr('http://cp.uctorrent.com', 'cp.utorrent.com')) {
echo "Good1";
}
else {
echo "Bad1";
}
Below $p has value 'false' (means 0), so it's >=0
$p = stripos('http://uctorrent.com','cp.utorrent.com');
You need to check stripos('http://uctorrent.com','cp.utorrent.com') !== false first then get $p (found position) like above...
utorrent <---> uctorrent
i am such an idiot...
it was spell mistake ...
comparing uctorrent with utorrent
sorry every one

Categories