how does php interpret numbers when you add '0' infront - php

How does the php interpret 026 and 26.
For example :
$a = 26;
echo $a; // result 26
$b = 026;
echo &b; // results in 22
Also for.
function abc() {
return (int)(0.5+029);
//return (int)(0.5+29)
}
$bac = abc();
it gives "2" and "29" does anyone knows how does return(int) works? and why does (return 029) displays "2".
P.S: Why does it says that my post does not meet quality standards and Things that i tried and proper grammat..????

When you preceed integers with zero in PHP, in that instance, 029.
It becomes octal.
So when you echo that, it will convert to its decimal form.
Which results to:
echo 029; // 2 (decimal)
Actually, its here stated in the manual
Valid octal:
octal : 0[0-7]+

Related

Why PHP number_format() change the number? [duplicate]

I am working with huge numbers for website purposes and I need long calculation. When I echo a long number I don't get the correct output.
Example
// A random number
$x = 100000000000000000000000000;
$x = number_format($x);
echo "The number is: $x <br>";
// Result: 100,000,000,000,000,004,764,729,344
// I'm not getting the value assigned to $x
Your number is actually too big for php standard integers. php uses 64 bit integers which can hold values within range -9223372036854775808 (PHP_INT_MIN)
to +9223372036854775807 (PHP_INT_MAX).
Your number is about 87 bits long which simply is too much.
If you really need such big numbers you should use the php BC math types, explained in the manual: http://php.net/manual/en/ref.bc.php
If you just want to format a string formed like a huge number then use something like this:
function number_format_string($number) {
return strrev(implode(',', str_split(strrev($number), 3)));
}
$x = '100000000000000000000000000';
$x = number_format_string($x);
echo "The number is: $x\n";
// Output: The number is: 100,000,000,000,000,000,000,000,000
Edit:
Added strrev() to function because the string needs to be reversed before splitting it up (thanks to #ceeee for the hint). This ensures that the delimiter is placed at right position when length of input is not divisible by 3. Generated string needs to be reversed afterwards again.
Working example can be found at http://sandbox.onlinephpfunctions.com/code/c10fc9b9e2c65a27710fb6be3a0202ad492e3e9a
answer #maxhb has bug. if the input is '10000000000000000000000' the out put would be:
The number is: 100,000,000,000,000,000,000,00
Which is incorrect. So try below code:
function number_format_string($number, $delimeter = ',')
{
return strrev(implode($delimeter, str_split(strrev($number), 3)));
}
$x = '10000000000000000000000';
$x = number_format_string($x);
echo "The number is: $x\n";
// Output: The number is: 10,000,000,000,000,000,000,000
The largest integer that can be represented in a 64bit PHP install, compared to your number:
9,223,372,036,854,775,808 - largest possible signed 64bit integer
100000000000000000000000000 - your number
since you're exceeding the maximum number size, you can't expect to get useful results without using something like gmp/bcmath.
PHP does not yet support formatting long numbers, even when you always keep them as strings in your code (to avoid issues with PHP’s int type):
php > echo number_format('100000000000000000000000000');
100,000,000,000,000,004,764,729,344
php > echo number_format('3.14159265358979323846', 20);
3.14159265358979311600
The underlying ICU library supports formatting arbitrary precision decimal numbers, but PHP doesn’t use the relevant function yet – see request #76093.
http://php.net/manual/en/function.number-format.php
That is the solution:
<?php
# Output easy-to-read numbers
# by james at bandit.co.nz
function bd_nice_number($n) {
// first strip any formatting;
$n = (0+str_replace(",","",$n));
// is this a number?
if(!is_numeric($n)) return false;
// now filter it;
if($n>1000000000000) return round(($n/1000000000000),1).' trillion';
else if($n>1000000000) return round(($n/1000000000),1).' billion';
else if($n>1000000) return round(($n/1000000),1).' million';
else if($n>1000) return round(($n/1000),1).' thousand';
return number_format($n);
}
?>

Big number base conversion and maximum limit

I have this problem:
// 7788778877887786660462040644602088666448 (10) = 9mwjmtyko1mpect1brz2exoncc (36)
$a = 7788778877887786660462040644602088666450 - 2;
echo $a."<br>"; // prints 7.7887788778878E+39
$b = "9mwjmtyko1mpect1brz2exoncc";
echo base_convert($b, 36, 10)."<br>"; //prints 7788778877887786660462040644602088666448
I know why $a was printed to 7.7887788778878E+39, it's because the value of $a is over the integer limit.
But, why it is not being 7.7887788778878E+39 when I convert 9mwjmtyko1mpect1brz2exoncc to decimal? It just prints the exact value (7788778877887786660462040644602088666448)
How can I print $a to the exact value (ignoring error)?
Is 7788778877887786660462040644602088666448 another data type that is bigger than long/unsigned-long?
Looking at some of the answers on this page:
How to keep long int during PHP string conversion?
I don't think you can do that because the large values are being stored as a float.
Suggestions are to use a library like BC Math and GMP, but I think that those are not enabled in PHP by default.

Bitwise and in PHP

I am very new face to PHP. I read that dechex(255) will give the corresponding hex value ff in PHP.
I need the hex value of -105. I tried dechex(-105) and got the result as ffffff97. But I just only want 97 to do some kind of stuffs.
In Java I know that a bit wise operation with 0xff gave us the result 97 that is (byte)-105 & (byte)0xff = 0x97.
Please to find the solution in PHP just like I've done in Java.
You can do it in php like this:
var_dump(dechex(-105 & 255))
to make it out of the final byte (example output below)
string(2) "97"
dechex() gives you a hexadecimal value for a decimal value between 0 and 2*PHP_INT_MAX+1 (unsigned int).
Anything below 0 or above 2*PHP_INT_MAX+1, will loop.
-105 is NOT 0xffffff97 , and it is not 0x97
0xffffff97 is 4294967191.
and 0x97 is 151.
If you want the hexadecimal representation of the negative number turned into a positive number, use the function abs().
$abs = abs(-105); // $abs becomes +105
$hex = dechex($abs); // $hex becomes 69
Either you want a binary negative value (ffffff97) or a signed value
// for a signed value
$i = -105;
if($i < 0)
echo '-'.dechex(abs($i));
else echo dechex($i);
If you want to remove front "f"
echo preg_replace('#^f+#', '', dechex($i));

Less than operator not working correctly in PHP 5.3.1

For the purpose of the example, var 2 is preset from a database as "147.22" type STRING. var 1 is calculated previously in the script and has 147.22 type FLOAT.
Script:
<?
$var1 = (float)$var1;
$var2 = (float)$var2;
var_dump($var1);
var_dump($var2);
if($var1 < $var2) { echo "hello"; }
?>
My expected results would be that the script NOT echo "hello" since the two values are equal in amount and type.
However here is the output I'm getting:
float(197.22)
float(197.22)
hello
If I do not mess with the types and leave the one a float and the other a string, then it still does not work (this is how I got here in the first place).
If i force the values at the time of execution like this:
$var1 = 147.22;
$var2 = 147.22;
var_dump($var1);
var_dump($var2);
if($var1 < $var2) { echo "hello"; }
?>
I get this, (and it works):
float(197.22)
float(197.22)
Notice no "hello"....
Anyone have any clue wth is going on here?
If one of the floats is calculated numerically, and one is created from string assignment, they could be different. Try the following:
$x = 147.22;
$y = 147.2200000000001;
printf("%.40f\n", $x);
printf("%.40f\n", $y);
var_dump($x);
var_dump($y);
var_dump($x < $y);
outputs
147.2199999999999988631316227838397026062012
147.2200000000001125499693443998694419860840
float(147.22)
float(147.22)
bool(true)
Cast them to a string with a specified precision for comparison.
If you are dealing with floats, then it's not safe to compare them directly, because there may be rounding or representation issues.
You'd better to check if the difference between those numbers is less than some predefined and very minimal epsilon, and then determine if they're equal, or which is the greater one.
This discussion may be worth reading: Is casting to float destructive?
EDIT:
More discussions to read:
PHP: Floating point numbers
php integer and float comparison mismatch

How to convert some character into numeric in php?

I need help to change a character in php.
I got some code from the web:
char dest='a';
int conv=(int)dest;
Can I use this code to convert a character into numeric? Or do you have any ideas?
I just want to show the result as a decimal number:
if null == 0
if A == 1
Use ord() to return the ascii value. Subtract 96 to return a number where a=1, b=2....
Upper and lower case letters have different ASCII values, so if you want to handle them the same, you can use strtolower() to convert upper case to lower case.
To handle the NULL case, simply use if($dest). This will be true if $dest is something other than NULL or 0.
PHP is a loosely typed language, so there is no need to declare the types. So char dest='a'; is incorrect. Variables have $ prefix in PHP and no type declaration, so it should be $dest = 'a';.
Live Example
<?php
function toNumber($dest)
{
if ($dest)
return ord(strtolower($dest)) - 96;
else
return 0;
}
// Let's test the function...
echo toNumber(NULL) . " ";
echo toNumber('a') . " ";
echo toNumber('B') . " ";
echo toNumber('c');
// Output is:
// 0 1 2 3
?>
PS:
You can look at the ASCII values here.
It does indeed work as in the sample, except that you should be using php syntax (and as a sidenote: the language that code you found most probably was, it did not do the same thing).
So:
$in = "123";
$out = (int)$in;
Afterwards the following will be true:
$out === 123
This may help you:
http://www.php.net/manual/en/function.ord.php
So, if you need the ASCII code you will need to do:
$dest = 'a';
$conv = ord($dest);
If you want something like:
a == 1
b == 2
.
.
.
you should do:
$dest = 'a';
$conv = ord($dest)-96;
For more info on the ASCII codes: http://www.asciitable.com/
And for the function ord: http://www.php.net/manual/en/function.ord.php
It's very hard to answer because it's not a real question but just a little bit of it.
But if you ask.
It seems you need some translation table, that defines links between letters and numbers
A -> 2
B -> 3
C -> 4
S -> 1
or whatever.
You can achieve this by using an array, where keys would be these letters and values - desired numbers.
$defects_arr = array(
'A' -> 2,
'B' -> 3,
'C' -> 4'
'S' -> 1
};
Thus, you can convert these letters to numbers
$letter = 'A';
$number = $defects_arr($letter);
echo $number; // outputs 1
But it still seems is not what you want.
Do these defect types have any verbose equivalents? If so, why not to use them instead of letters?
Telling the whole story instead of little bit of it will help you to avoid mistakes and will save a ton of time, both yours and those who to answer.
Out of this question, if you are looking for convert RT0005 to 5
$max = 'RT0005';
return base_convert($max,10,10);
// return 5

Categories