I am trying to return the data that is true from my database using this mysqli statement
$resulttwo = mysqli_query($link, "
SELECT
*
FROM
Events
WHERE
time >= '%$lasttime%'");
The variable "lasttime" is an float of a really high value. The numbers in the database are all below below this number. However, when I use this statement, it returns all rows in the database. I do not understand why using this comparison yields incorrect results. Is there something that I am missing?
'%$lasttime%' is a string, that equates to zero when used in a numeric comparison. time may be a string column, as you say, but gets successfully cast numeric when you use >=. if $lasttime equaled 12345 and $time equaled 54321, then the comparison is 54321 >= '%12345%' which equates to 54321 >= 0, as '%12345%' doesn't get converted to an int.
To test:
select '%12345%' = 0, '12345' = 0
returns TRUE (because that's a non-numeric string, whose value is 0) and FALSE (because that string can be converted to 12,345, and not equal to 0).
I have no idea why you want to use wildcards in this query.
Related
<?php
$number1 = 1;
$number2 = 0;
$number3 = $number1/$number2 ;
$number3 will return float(INF) which is OK.
var_dump($number3 );
in order to identify INF, I can use is_infinite like below.
$check_Is_INF = is_infinite($number3);
$check_Is_INF above will return true - which is OK.
var_dump($check_Is_INF );
But why does this always return TRUE if I check this string: 592e5399, like below
$number3 = '592e5399';
$check_Is_INF = is_infinite($number3);
var_dump($check_Is_INF );
As in the documentation for is_infinite:
Returns TRUE if val is infinite (positive or negative), like the
result of log(0) or any value too big to fit into a float on this
platform.
The number 592e5399 is in notation expression (because of the e that it contains), so it's a very huge number exceeding the float limit. Because of that it will be considered an infinite number.
Precisely, the e means ^ so the number will be 592 ^ 5399.
A note about the type of the variable $number3:
you set it as String but thanks to the PHP's Type Juggling, every value you use in an expression, will assume the type accordingly to the context when is it. In this case, the String is converted in number because the function is_infinite expects a number.
Because PHP is casting the string into a float
Function description from https://www.php.net/is_infinite
is_infinite ( float $val ) : bool
It doesn't matter if you call
is_infinite('592e5399')
is_infinite(592e5399)
Both will be cast to float, and you'll get the same response: true.
I have a problem with this function always my mt_rand() give me the same number:
$hex = 'f12a218a7dd76fb5924f5deb1ef75a889eba4724e55e6568cf30be634706bd4c'; // i edit this string for each request
$hex = hexdec($hex);
mt_srand($hex);
$hex = sprintf("%04d", mt_rand('0','9999'));
$hex is always changed, but the result is always the same 4488.
Edit
$hex = str_split($hex);
$hex = implode("", array_slice($hex, 0, 7));
mt_srand($hex);
$number = sprintf("%04d", mt_rand('0','9999'));
http://php.net/manual/en/function.mt-srand.php
Your problem is, that you always end up with a float value in your variable $hex. And the function mt_srand() as you can also see in the manual:
void mt_srand ([ int $seed ] )
Expects an integer. So what it does is, it simply tries to convert your float value to an integer. But since this fails it will always return 0.
So at the end you always end up with the seed 0 and then also with the same "random" number.
You can see this if you do:
var_dump($hex);
output:
float(1.0908183557664E+77)
And if you then want to see in which integer it will end up if it gets converted you can use this:
var_dump((int)$hex);
And you will see it will always be 0.
Also if you are interested in, why your number ends up as float, it's simply because of the integer overflow, since your number is way too big and accoding to the manual:
If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.
And if you do:
echo PHP_INT_MAX;
You will get the max value of int, which will be:
28192147483647 //32 bit
9223372036854775807 //64 bit
EDIT:
So now how to fix this problem and still make sure to get a random number?
Well the first thought could be just to check if the value is bigger than PHP_INT_MAX and if yes set it to some other number. But I assume and it seems like you will always have such a large hex number.
So I would recommend you something like this:
$arr = str_split($hex);
shuffle($arr);
$hex = implode("", array_slice($arr, 0, 7));
Here I just split your number into an array with str_split(), to then shuffle() the array and after this I implode() the first 7 array elements which I get with array_slice().
After you have done this you can use it with hexdec() and then use it in mt_srand().
Also why do I only get the first 7 elements? Simply because then I can make sure I don't hit the PHP_INT_MAX value.
<?php
// your code goes here
if (0 == "asdf")
echo "same";
else
echo "not same";
Hello, Why this code prints "same", not "not same"? I'm little bit confused about this weird result. Is this a bug?
Execution Result: see http://ideone.com/wfWRlq
No, this is not a bug the string just get's converted to a int. It converts it from left to right until a non numeric value. So since there is a non numeric value right at the start it gets converted to 0.
For more information about String to int see the manual: http://php.net/manual/de/language.types.string.php#language.types.string.conversion
And a quote from there:
The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).
So as an example to show that:
echo "5xyz" + 5; // 5 + 5 -> 10
//^
echo "xyz5" + 5; // 0 + 5 -> 5
//^
echo "x5z" + 5; // 0 + 5 -> 5
//^
You should use ===. Because that do convert type of value.
That's one of non-intuitive behaviors of comparisons in PHP. There's == operator for loose comparison, what checks only values of given variables and === operator for strict comparison, what checks also types of variables. PHP manual has dedicated page with comparisons tables.
in php.net the following is written:
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value
var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> true
why in the first example it is evaluated as true but, the statement $num = (int)"1e1" ; is evaluated as 1 and not 10???
furthermore, why in the second example it is evaluated as true but the statement $num = (int)"1e2" ; is evaluated as 1 and not 100??
I am not sure why (int)'1E1' displays 1 (it probably ignores all letters and anything after), but what works perfectly for me is this:
echo '1E1'*1; //returns 10
String and string comparison: "10" is the same string representation as "1e1".
Int and string comparison: 100 as string is "100" or "1e2" and is the same representation as "1e2";
Casting string to int: (int) "1e1" => 1 as intval("1e1").
Casting is not the same as equality.
1e1 is double.
var_dump("10" == "1e1");
// 10 (converts to real type, not int) == 10 (converts to real type, not int) -> true
My attempt to convert the numeric string (float type) to integer:
(int)'1E1'
It turns 1 instead of 100.
How can the below be possible:
$varnum = 4;
if( $varnum/4 - floor($varnum/4) !== 0){
echo 'foo';
}
This echoes 'foo' on my server running PHP 5.1.6. If i change the operator to == I get the same results.
I have no idea why, but could it possibly be because "==" is "equals" and "!==" is "Not identical"? How then would I make them identical? I guess in javaScript I would "parseInt", but there is no such thing in PHP, right?
The reason this fails is because in PHP, the floor function returns a float, despite the fact that the value is always a whole number. You can see this in the documentation here: http://php.net/manual/en/function.floor.php
You're doing a fixed type comparison of that float to an integer zero, so the result is false, regardless of whether the value is actually zero.
To fix this, either:
cast the output of floor to an integer - either intval(float(...)) or (int)float(..)
use != instead of !==.
use 0.0 instead of just 0 to compare against.
In case you're wondering why floor() would return a float rather than an integer, it's because the input is a float. The float data type has a larger possible range than integer, and thus it is possible to call floor() on a value that would be too big to hold in an integer. Therefore it would not be safe for the function to return an integer; it returns a float instead so that it can guarantee the result will be correct.
It may seem odd at first glance, but hopefully that explains the logic behind it for you.
What is it you are trying to accomplish? If you are trying to see if $varnum is divisible by four then use modulus, so...
$varnum = 4;
if ($varnum % 4 != 0) {
echo "foo - $varnum is divisible by 4";
}
You original post should use '!=' versus '!==', like this:
$varnum = 4;
if( $varnum/4 - floor($varnum/4) != 0){
echo 'foo';
}