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.
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.
<?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.
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.
Why this FALSE condition is TRUE?
<?php
if(111111111111111119 == 111111111111111118)
{
echo 'Condition is TRUE!';
}
?>
Quote from:
http://php.net/manual/en/language.operators.comparison.php
$a == $b is TRUE if $a is equal to $b after type juggling
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
So because your strings are both numeric they are being converted to numbers first.
Then on some architectures numbers are so big that are overflowing maximum integer size and you are getting equal results.
PHP DOC
Converting to string
An integer or float is converted to a string representing the number textually (including the exponent part for floats). Floating point numbers can be converted using exponential notation (4.1E+6).
Converting to integer
If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31 on 32-bit platforms and +/- 9.22e+18 = 2^63 on 64-bit platforms), the result is undefined, since the float doesn't have enough precision to give an exact integer result. No warning, not even a notice will be issued when this happens!
My Guess you are using a 32 bits system so therefore
var_dump(111111111111111119,111111111111111118);
var_dump(111111111111111119 === 111111111111111118); // would be true on 32bit
Output
float 1.1111111111111E+17
float 1.1111111111111E+17
true
Simple Solution
if(bcsub("111111111111111119", "111111111111111118") == "0")
{
// 32 bit true
var_dump("Am Free");
}
since it's converted into a numeric value
if('111111111111111119' == '111111111111111118')
{
echo 'Condition is TRUE!';
} else {
echo 'Condition is FALSE!';
}
// on 64 bit: condition is FALSE! (tested on my mac)
I'd assume that on 32bit machine it'd be true. Even when i remove the quotes on my mac it's shows false.
if('a111111111111111119' == 'a111111111111111118')
{
echo 'Condition is TRUE!';
} else {
echo 'Condition is FALSE!';
}
// condition is FALSE!
I was reading some code that a consultant provided us. It's a bit convoluted, and at the end of a function, it reads:
return (int) 1;
Instead of:
return 1;
PHP has a lot of magic in it; is this as bone-headed as it looks, or is there a valid reason to cast an integer as an integer?
No, it's the same. 1 is an integer literal.
See here these are all integer literals; casting to int has no effect:
$a = 1234; // decimal number
$a = -123; // a negative number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
If you did return "1"; that would be an entirely different matter. There are some differences in behaviour between "1" (string) and 1 (int), namely with bitwise operators.
It's pretty bone headed. Integer literals are, well... integers.
1 === 1 however 1 !== '1'
also, when necessary, (as in this case it definitely isn't) I would suggest not typecasting with (int) use intval() instead.