Un-expected output from is_numeric / foreach routine - php

Ref: http://php.net/manual/en/function.is-numeric.php.
Example #1 is_numeric().
Why does the Example #1 routine output 1337 4 times instead of 1 time? My expectation is the routine would ouput the other 3 numeric values that follow 1337. Even if I re-arrange the order of the elements it still outputs 1337 4 times. I understand hex and binary values are not allowed, but why does the routine output 1337 for those values. Also, if I change 1337 to 01337 the ouput is 735.
Why these confusing outputs?
<?php
$tests = array(
"42", 1337, 0x539, 02471, 0b10100111001, 1238.443e2, "not numeric",
array(), 9.1, null
);
foreach ($tests as $element) {
if (is_numeric($element)) {
echo var_export($element, true) . " is numeric", PHP_EOL;
} else {
echo var_export($element, true) . " is NOT numeric", PHP_EOL;
}
}
?>
Output:
'42' is numeric
1337 is numeric
1337 is numeric
1337 is numeric
1337 is numeric
123844.300000000002910383045673370361328125 is numeric
'not numeric' is NOT numeric
array () is NOT numeric
9.0999999999999996447286321199499070644378662109375 is numeric
NULL is NOT numeric

Base-N notation syntax
which you had to know to pull this off
Hexadecimal
A number prefixed with 0x will be interpreted as base-16
Octtal
A number prefixed with 0 will be interpreted as base-8
Binary
A number prefixed with 0b will be interpreted as base-2
Conversion to Decimal
1337 base10 (base10 value: 1337)
0x539 base16 (base10 value: 1337)
02471 base8 (base10 value: 1337)
0b10100111001 base2 (base10 value: 1337)
Numbers in strings are always shown in base-10
Converting these numbers to a string, as with var_export, will convert them to base-10, unless you use number_format.

Because that is your values.
For example, if you start a number with a zero, it is means, this number is in octal number system. 02471 = 1337 in decimal number system. The other is the hexa, the remaining the binary.

Related

Why PHP round my number when I use json_encode?

I need execute json_encode() and convert my original number from:
50610101800060384093800100001010000000056199999999
to
"50610101800060384093800100001010000000056199999999"
But it return
5.061010180006E+49
I tried this:
ini_set('precision', 30); //With 1, 30, 50, 100, 1000
ini_set('serialize_precision', -1);
'content' => json_encode($params, JSON_NUMERIC_CHECK)
but doesn't work. Can you help me?
50610101800060384093800100001010000000056199999999 exceeds the value of the maximum integer in PHP and so it is promoted to a float and expressed in scientific notation. The float result may be problematic for various reasons as the Manual explains in warning about floating point precision.
If you wish to express the value as if it were an integer you must encapsulate it in a string. That string you may add zero to it but when you do so the result in scientific notation will refer to a float, as follows:
<?php
$s = "50610101800060384093800100001010000000056199999999";
echo $s,"\n";
$x = $s + 0;
echo $x, "\n",is_float($x);
See here.
For more info in re PHP and floats, see here.
On the other hand, if there were an array of numbers whose digits corresponded to the numerical display in the OP's post, you could write code as follows:
<?php
$a = [5,0,6,1,0,1,0,1,8,0,0,0,6,0,3,8,4,0,9,3,8,0,0,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,5,6,1,9,9,9,9,9,9,9,9];
foreach($a as $e) {
$e = (string) $e;
}
$foo = join($a);
var_dump($foo);
$foo = bcadd($foo, 1);
var_dump($foo);
See live code.
The reason this example works is because each array value is converted to a numerical string and then the individual elements are joined to form one very long numerical string. BC Math is an extension in PHP which supports arbitrary precision. In this case, the bcadd() function adds one to the numerical string which results in the display of an incremented numerical string value.
Try This [https://3v4l.org/biNJG][1]
If you want this output "50610101800060384093800100001010000000056199999999"
you may want to pass this Value as string after encoding the value to JSON using json_encode
An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.
Rules for integers:
An integer must have at least one digit
An integer must not have a decimal point
An integer can be either positive or negative
Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)

Why only hex recognized but not octal and byte if the left hand operand is a number in an expression

First expression:
displays 123 octal, is not recognized, if recognized it should be 83
Second Expression:
displays 291, here hex recognized, if not recognized it should be 123
Third Expression:
Displays 0
$y = 0+"0123";
echo $y;
echo '<br>';
$x = 0+"0x123";
echo $x;
echo '<br>';
$x = 0+"0b10101";
echo $x; // This displays 0
output:
123
291
0
See the documentation at http://php.net/manual/en/language.types.string.php#language.types.string.conversion
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). Valid numeric data is an
optional sign, followed by one or more digits (optionally containing a
decimal point), followed by an optional exponent. The exponent is an
'e' or 'E' followed by one or more digits.
There is no mention of non-decimal support.
PHP casting of strings to numbers is limited (and probably with good reason). If you want to use different base numbers you need to specify them as numbers e.g.
$y = 0123;
echo $y; // 83
$x = 0x123;
echo $x; // 291
$x = 0b10101;
echo $x; // 21
Notice that they are not quoted.
If you want to explicitly convert strings you need to do the following:
echo octdec("0123"); // 83
echo hexdec("0x123"); // 291
echo bindec("0b10101"); // 21
The prefixes (e.g. 0 or 0x or 0b) are allowed but optional when using these functions.
Note: PHP used to support implicit casting of hex strings as numbers but as of 7.0 it does not

PHP Manual: Number Conversion in Is_Numeric Example 1?

I ran across this example in the PHP documentation:
<?php
$tests = array(
"42",
1337,
0x539,
02471,
0b10100111001,
1337e0,
"not numeric",
array(),
9.1
);
foreach ($tests as $element) {
if (is_numeric($element)) {
echo "'{$element}' is numeric", PHP_EOL;
} else {
echo "'{$element}' is NOT numeric", PHP_EOL;
}
}
?>
Output:
'42' is numeric
'1337' is numeric
'1337' is numeric
'1337' is numeric
'1337' is numeric
'1337' is numeric
'not numeric' is NOT numeric
'Array' is NOT numeric
'9.1' is numeric
The five examples after '42' all evaluate to '1337'. I can understand why this is the case for '1337e0' (scientific notation), but I don't understand why that is the case for the rest of them.
I wasn't able to find anyone mentioning it in the comments of the documentation and I haven't found it asked here, so could anyone explain why '0x539', '02471', and '0b10100111001' all evaluate to '1337'.
When outputting all numbers get converted to normal representation. Which is decimal number system, and non-scientific notation (e.g. 1e10 - scientific float).
Hex:
Hex numbers start with 0x and are followed by any of 0-9a-f.
0x539 = 9*16^0 + 3*16^1 + 5*16^2 = 1337
Octal:
Octal numbers start with a 0 and contain only the integers 0-7.
02471 = 1*8^0 + 7*8^1 + 4*8^2 + 2*8^3 = 1337
Binary:
Binary numbers start 0b and contain 0s and/or 1s.
0b10100111001 = 1*2^0 + 1*2^3 + 1*2^4 + 1*2^5 + 1*2^8 + 1*2^10 = 1337
They are octal, hexadecimal and binary numbers.
http://php.net/manual/en/language.types.integer.php

php Determine a float variable has two decimal places

MySQL data imoprt mongo database.
price float(15,2) in mysql, mongo is not float(15,2).
I want to Determine a var $price have two decimal places.
eg. 100.00 is right, 100 or 100.0 is wrong.
eg.1
$price = 100.00;
$price have two decimal, it's right.
eg.2
$price = 100.0;
$price have not two decimal, it's wrong.
I like to use Regular Expressions to do these things
function validateTwoDecimals($number)
{
if(preg_match('/^[0-9]+\.[0-9]{2}$/', $number))
return true;
else
return false;
}
(Thanks to Fred-ii- for the corrections)
Everybody is dancing around the fact that floating point numbers don't have a number of decimal places in their internal representation. i.e. in float 100 == 100.0 == 100.00 == 100.000 and are all represented by the same number, effectively 100 and is stored that way.
The number of decimal places in this example only has a context when the number is represented as a string. In which case any string function that counts the number of digits trailing the decimal point could be used to check.
number_format($price, $numberOfDecimalDigits) === $price;
or
strrpos($price, '.') === strlen($price) - 1 - $numberOfDecimalDigits;
Trivia: $price should not be called a "float variable". This is a string that happens to represent a float value. 100.00 as a float has zero decimal digits, and 100.00 === 100 as float :
$price = 100.00;
echo $price; // output: 100
$price2 = (float)100;
echo $price === $price2; // ouput: 1
In order for this to work, the number will need to be wrapped in quotes.
With the many scripts I've tested, using $price = 100.00; without quotes did not work, while $price = 100.10; did, so this is as best as it gets.
<?php
$number = '100.00';
echo $number.'<br>';
$count = explode('.',$number);
echo 'The number of digits after the decimal point is: ' . strlen($count[1]);
if(strlen($count[1]) == 2){
echo "<br>";
echo "There is 2 decimal points.";
}
else{
echo "<br>";
echo "There is not 2 decimal points.";
}
After you format the value, you can check with simply splitting the value as string into 2 parts, for example with explode ...
$ex=explode('.',$in,2); if (strlen($ex[1])==2)
{
// true
}
else
{
// false
}
But again, as i've commented already, if you really have floating input, this is just not a reliable way, as floating numbers are without set decimal places, even if they appears so because of the rounding at the float=>string conversion
What you can do, if you really have floating numbers and wish to have xxx.yy format numbers:
1) convert float to string using round($x,2), so it will round to 2 decimal places.
2) explode the number as i've described, and do the following:
while (strlen($ex[1]<2)) {$ex[1].='0';}
$number=implode('.',$ex);
I would use the following function for that:
function isFloatWith2Decimals($number) {
return (bool) preg_match('/^(?:[1-9]{1}\d*|0)\.\d{2}$/', $number);
}
This will also check if you have only one leading 0 so number like 010.23 won't be considered as valid whereas number like 0.23 will.
And if you don't care about leading 0 you could use simpler method:
function isFloatWith2Decimals($number) {
return (bool) preg_match('/^\d+\.\d{2}$/', $number);
}
Of course numbers need to be passed as string - if you pass 100.00 won't be considered as true, whereas '100.00' will

Why this FALSE condition is TRUE?

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!

Categories