PHP var becoming negative - php

I have this code :
<?php
$integer = 33963677451;
$integer &= 0xFFFFFFFF;
echo $integer;
?>
But, when I execute it, the output is
-396060917
The same function in Python
if __name__ == '__main__':
integer = 33963677451
integer &= 0xFFFFFFFF
print integer
The output is
3898906379
It seems PHP can't store big variables. Do you guys have any idea how to get the same result in PHP I have in Python ?
Thanks !

That value is too big for an integer, python dynamically casts that into a long variable. INT_MAX defines how big your integer can be before it switches over into a double. If you want to manipulate large numbers without quirks use GMPLIB a multiple precision arithmetic library.

Your 33,963,677,451 is above what a signed 32bit integer supports (max 2,147,483,647). 64bit PHP versions do support higher values.

php only supports signed integers. You are seeing overflow
also, the answer for the python case should be 33963677451

I don't know if it's just a typo, but you are using different values when performing the AND operation in each language. :)
You have "0xFFFFFFFFFFF" (11 chars) in your PHP version, as opposed to only 8 in the Python version.
Either way, it should be integer overflow.

<?php
$integer = (float) 33963677451;
$integer &= 0xFFFFFFFFFFF;
echo $integer;
try making sure that $integer is of type float.
for more: http://www.php.net/manual/de/language.types.integer.php#language.types.integer.overflow

If you wish to see whats happening, here is a printf statement showing the different types.
$i1 = 33963677451;
$i2 = 0xFFFFFFFFFFF;
printf("%b (%d, %f) & \n%b (%d, %f)\n = \n%b (%d, %f)",
$i1, $i1, $i1, $i2, $i2, $i2, $i1&$i2, $i1&$i2, $i1&$i2);
PHP casts integers larger than 32 bits to floats. It seems that you can not perform this operation on floats.

Related

How to parse a String to a long with no loss

I'm having an issue when parsing a string to a long. I want the variable "695690829980893234" to be parsed to a long without any loss. I've looked around for a bit but nothing really seemed to have helped.
When using (double) $var it shows as a 4.2904753172133E+17 (which is not the way I need it)
Does anyone have any ideas? Much appreciated!
In standard PHP, the type long does not exist.
So, if you want to have a long integer, you may use the standard int type, or use an extension, such as BC or GMP.
Specifically, the value 695690829980893234 is in the integer range of 64bit PHP, but out of the integer range of 32bit PHP.
To convert to int:
First, please make sure the number is in the int range. Then just convert
$res = intval("695690829980893234");
if($res<PHP_INT_MAX){
// OK! inside the positive limitation
}
To convert to GMP
A GMP object represents an arbitrary length integer:
$res = gmp_init("695690829980893234");
Then, use GMP functions to operate the number, such as:
$res = gmp_add($res, gmp_init("100"));
$res = gmp_sub($res, gmp_init("200"));
and finally, convert it back to int or string:
$int = gmp_intval($res);
$str = gmp_strval($res);
Hope this answer helps.
Be sure you're running 64-bit version of PHP.
echo PHP_INT_SIZE;
8
Then it will cast string to int easily
$a = "695690829980893234";
$b = (int)$a;
var_dump($a, $b);
string(18) "695690829980893234"
int(695690829980893234)

PHP - strange behavior with unsigned integers (64bit) [duplicate]

I understand Integer size, PHP_INT_MAX is platform dependent.
On 64-bit system I can get a:
$large_number = 9223372036854775807
From what I read from PHP Document Example 3 here, when integer overflows, PHP handles integer as float with a precision of roughly 14 decimals digits, i.e:
Unsigned Int64: 18446744073709551615
PHP will handles as: 1.844674407371E+19
So it seems all precision only kept up to the max of Signed Int64.
Is this really just a limitation of PHP and there is nothing I can do about it?
This is pretty hacky, and I assure you your life will be easier using GMP but it is possible using pack() and a little binary thinking:
<?php
$large_number = 9223372036854775807;
$left = 0xffffffff00000000;
$right = 0x00000000ffffffff;
$l = ($large_number & $left) >>32; //shift 32 bits left
$r = $large_number & $right;
$int64_binary_string = pack('NN', $l, $r);
I haven't tested this, and wouldn't use it in real life, but it's probably close to what you're looking for.
http://tw1.php.net/manual/en/function.pack.php
if you need precision or numbers outside of PHP_INT_MAX / PHP_INT_MIN, learn to use GMP, which will probably keep going until you run out of memory... to give you some idea of how far you can take it,
$large_number = gmp_init("9223372036854775807");
$i=0;
while(true){
echo 'iteration '.$i++.':'.gmp_strval($large_number).'*1337=';
$large_number = gmp_mul($large_number,"1337");
echo gmp_strval($large_number).PHP_EOL;
}
will eventually
iteration 1291:63173727399431265977452467325176816266767500735669001788445953704
99100022309199341754638029233101065727095873054512847649411469159231474984853877
29990385564525384931210393372816147935077346316357322846070505282141866990047417
69795035320095201175780336777052639896516690649784147989039497959127394363606555
18277493016490689369408018504673663458745651065704848764945821685549595325229654
78521759046816461228651950877052534907426557036526546565508337234675673499891633
29265311281809541141691613057522094435618917713573494707844587004419303204290366
80410319411622496279245084808349790649184389864091281089934776245296259423173088
37125998364710920001673776690982404095905353172292803413540568804924330209161221
68502039031454119485450113377479892509776976500478875924900269471621353800015849
16768619446056453580306524878177680668058129358555807454509042635315136209751306
28888008462882394248192470687562440408441365704520299664521180552207604024174563
43137016510143518117853956283018500610190006192251441907450531358798897519852285
89712288688300401723956266383086427686519451058576140512867416675873224992592868
63873656820691379608416040082157797343590944870890093034489537997624441231196880
03716995482080107872348544579795198224539076738023147659657987086535844760748293
51680757422559231216506109885022428024013042708325323490543266063675126192858475
51062320509059668456301511665702512575733290996693385011230330439062943182094596
72982868367513131107811463983324141985257032192551001490775273440704964956322520
07994322409745388858643755238449137799970426027598547825636867768578263720009398
06345603047100479826296034929890108340101488966701293667796312958087571285537092
98292308718879824518810679195685709967212889579496467906343837737351387116485926
96065604380646092415099392666166947273008556519323980631398826013377061548412499
37045775535278706790702274483174254245054418225127782273776637425449169726435461
00864900455001093729251741310689643202116980745998974559375404087598011323913589
37378336888032750432647317316783507777959680594838416103235878202519306235270714
07689264877781232001726371838121263807804664374737613209359850417045192588050394
70657700599839677955302104699239419723871651195923433991338871172082351085772713
85795544605616579911291449220540413076513145849439424174121530477862582225947332
71577283620910275735801384726436002832118799836996365693738642178527175171378406
17487708039971384270639342138853231882316669599224158264716457232654635304792216
85595639961619807370286817974879544351592128186367207420223323272766972084744559
66627509594039497325711708953969586289133165339724320993714241576206582692070672
87281148490183467979899908277488360099016745185705207507885725820344193088663031
74742220273886082811546338253396232117540977001317286430939919216858700761871025
04883648220800173612755757031087544792419044177699287860855715222255510614135023
30706372831160632288499262660942154311052431900104315094339186786507006013357031
08125929293885129152264099174616004246743029176792527824811466605161478045492703
27090040582587712800567514734685257659124144431850835544283278381310315791931357
63459289106249506321560095185194943714045276693796155870508190654607382917421646
15486864936613368395909757833185705018554557948762713677856056206953385065593915
49326215023945424937441813782143778143766220175505533500485707438756395764163323
47714668439588417016028256182365660677880890399143415948539281432076160651247693
72982310275084829961404187585100633245654443972233743905144338234758859827413170
88983027723373315156469586730671906768407758289550985343535503818941377020797426
18604249085624012174154963219214543971133251713032811649089587541472009254502083
21938426925234679535003649815691245256828585977342824147515002509080267191887689
93756807224396178461744274960675671607151017304225081498878127594506757910634207
82366257650930768410626608502254524385838019829644847875689142861722171975269086
27152913039908221672006718321592434013558052896236283594560899204934144043552210
8813052796446077685440711695302241857244755302464656726076289075058391*1337=8446
32735330396026118539488137614033486681484835894553911522401035729672982739951992
59510450846561248771271822738836773072631342658924820547496339499714549977043965
30282959394551897891984120249697406451962655622236761656933974621596222296728397
20183102709193795416428153987614058613458087713533262641419642793700816304805168
68985207407486880443429354748473827987325635935798089498320484478359184559360866
27076583226192391712293067578359927580846468827613754693551137122772118377935650
64416866579070402604224929830477624243882128249086083841362204170859705333927752
53506783887636700979595292482900428172427958399610988487824191523745981361850004
22378394358434742762254571913554781639037404921838294896485533928722618505415775
20468015856906162855718175811402571115916602835577500306211903371964419937747843
68698237621235590531937189523891145666785900034163371124374965082326731487376110
98333333092709828260861059469436406514648183983015665803213913077419107406188372
35707395503957353158240382790401778302613604267141259840425062444532997625763710
49295281541865538168765060653162998657037360956425018150966653699907916926437453
64522455898449750483810932923800543871125123028238779261102286096962295954110422
53300041031861800262087455987369484209627287346984244451204684319717267396169213
64686689162749862681054381010309575068563467271336437198517817577032252061277672
60751210970442593137554100625790557600149517970271550344604758277809500736505629
11439273457043778342886520414406889931665405902225381466032093468840906182958490
40067007538064972385604595988992584428764922065891385936525652108407127397334152
77577987012630748507156907484796296338436704249630828087630933181681675714232538
16498780846317942261636333677867775907817110548388045747416843463971305692382555
89878879946652085040124400663361621041802303798851312902275116583020189066763097
91689409840039779256377571669958449000393642378255399242442113685637190833646231
60095781323920529612304032574006289858849152651185411400724689927483641929978732
84494632525395498991320929552989623300263691567683124365569447208054714159350718
63081591475681297110348362690241888609141200075894224902233777226934570198564942
62389139828831041708163976489496312464200707570741034016781184280864313770936734
13966676078625322832980760007005101208004862489022724360915838409882820115703865
87664513792449357865428353820641409325285645926908332041329290558106564944174076
98448004396467710266573872541626995999259033200592474025071939364137062868568245
40734756324139507980786753851729563208385832156894416773034762738098032723080792
44765548714573368685710420592114171685959409873882010592984896309489553137529668
91261773670019374523853883132878624380432154218001861595424734463034850618569271
90374542447907623411522862507612119581666719929400829186215604902943767120983212
02544471505640473874642620655839478699640912521556176910985261615442047526176536
97235141776796603138771014504394692811314927335598670398583505556436746592441767
65771005964615976778954300093716097017729308511008961468237442731938425891977201
43587672002741894902489811053845671227067431958118922138122251574506953505558995
19258472626056397456785349396054603988694509052100709605927409090593842025207354
53313462229692876098074439774957481872935471486966758326990650144914948701503314
13597050267262313782154363746508982901493908456173011366863634889451170372971355
04297785158228883263267504636547471231970192746858267907181665167734883778841765
83973988012795466494399915908765156011779802198725955892514094797030806615012236
41998374589083393493611728331296674043069686059246210768061588107388102747930427
68451858240898452894051575403248691748327785429480763732692852643167679903876653
82998798035791949083798194517073558852275583546403172355538414465285125901769060
33520956224233729387609101357489339640000565938555353265179358602368647929443736
50077755675142991038654325122351616097963840061225439309347683450344473435729237
54729823959690842761271167222679111659279222369969505862293059483051588848405865
434231536619097363136237839395246042763998493353068767
*and by "eventually", i mean less than 5 seconds on my laptop..
Yes it is a limitation of PHP and there is nothing you can do about it short of recompiling your PHP interpreter. Even then you are limited to the types your native system supports which wouldn't be bigger than 64bit normally. You can, as you know, use GMP, or BCMath, but that is not what your asking.
Under the hood, depending on your system the PHP integer and PHP floating point types corresponds to a signed C integer type, and C float types (PHP always uses C doubles for 'floats' AFAIK). This is a static relationship and can't change after compile time. Since the C types have fixed precision of course the PHP ones do too.
The "overflow" into a float is just a convenient compromise so you can store really big numbers, rather than not at all. Your losing some precision yeah, but only in the significand. PHP is not going to automatically convert the number to some other bigger precision floating point format because it doesn't have one.

PHP: Does BCMath accept integers?

Is it safe to use INT-type variables in BCMath functions in PHP ?
Example:
<?php
$a = 1;
$b = "1";
echo bcadd($a,$b,0);
?>
This seems to work but is it safe to do this ? Or is there for example a risk that PHP can interpret an INT as something else than it's int value (I'm thinking about hexadecimal values etc) ?
Thanks !
Be careful with very large numbers. If you assign a literal integer > PHP_INT_MAX to a variable, PHP will automatically convert it to a float. I know you specifically asked about ints, and in that case you'd be passing a float, but if you don't know that about the automatic conversion it looks like you're using a large int, so I thought it worth mentioning.
$a = 9223372036854775808;
$b = '1';
var_dump($b); // float 9.2233720368548E+18
echo bcadd($a, $b, 0); // echoes 1
Basically, the function does take a string. If you give it something that isn't a string, PHP will automatically convert it to a string if it can, unless you have enabled strict mode.

Is it possible to use a large unsigned int64 without losing precision in PHP?

I understand Integer size, PHP_INT_MAX is platform dependent.
On 64-bit system I can get a:
$large_number = 9223372036854775807
From what I read from PHP Document Example 3 here, when integer overflows, PHP handles integer as float with a precision of roughly 14 decimals digits, i.e:
Unsigned Int64: 18446744073709551615
PHP will handles as: 1.844674407371E+19
So it seems all precision only kept up to the max of Signed Int64.
Is this really just a limitation of PHP and there is nothing I can do about it?
This is pretty hacky, and I assure you your life will be easier using GMP but it is possible using pack() and a little binary thinking:
<?php
$large_number = 9223372036854775807;
$left = 0xffffffff00000000;
$right = 0x00000000ffffffff;
$l = ($large_number & $left) >>32; //shift 32 bits left
$r = $large_number & $right;
$int64_binary_string = pack('NN', $l, $r);
I haven't tested this, and wouldn't use it in real life, but it's probably close to what you're looking for.
http://tw1.php.net/manual/en/function.pack.php
if you need precision or numbers outside of PHP_INT_MAX / PHP_INT_MIN, learn to use GMP, which will probably keep going until you run out of memory... to give you some idea of how far you can take it,
$large_number = gmp_init("9223372036854775807");
$i=0;
while(true){
echo 'iteration '.$i++.':'.gmp_strval($large_number).'*1337=';
$large_number = gmp_mul($large_number,"1337");
echo gmp_strval($large_number).PHP_EOL;
}
will eventually
iteration 1291:63173727399431265977452467325176816266767500735669001788445953704
99100022309199341754638029233101065727095873054512847649411469159231474984853877
29990385564525384931210393372816147935077346316357322846070505282141866990047417
69795035320095201175780336777052639896516690649784147989039497959127394363606555
18277493016490689369408018504673663458745651065704848764945821685549595325229654
78521759046816461228651950877052534907426557036526546565508337234675673499891633
29265311281809541141691613057522094435618917713573494707844587004419303204290366
80410319411622496279245084808349790649184389864091281089934776245296259423173088
37125998364710920001673776690982404095905353172292803413540568804924330209161221
68502039031454119485450113377479892509776976500478875924900269471621353800015849
16768619446056453580306524878177680668058129358555807454509042635315136209751306
28888008462882394248192470687562440408441365704520299664521180552207604024174563
43137016510143518117853956283018500610190006192251441907450531358798897519852285
89712288688300401723956266383086427686519451058576140512867416675873224992592868
63873656820691379608416040082157797343590944870890093034489537997624441231196880
03716995482080107872348544579795198224539076738023147659657987086535844760748293
51680757422559231216506109885022428024013042708325323490543266063675126192858475
51062320509059668456301511665702512575733290996693385011230330439062943182094596
72982868367513131107811463983324141985257032192551001490775273440704964956322520
07994322409745388858643755238449137799970426027598547825636867768578263720009398
06345603047100479826296034929890108340101488966701293667796312958087571285537092
98292308718879824518810679195685709967212889579496467906343837737351387116485926
96065604380646092415099392666166947273008556519323980631398826013377061548412499
37045775535278706790702274483174254245054418225127782273776637425449169726435461
00864900455001093729251741310689643202116980745998974559375404087598011323913589
37378336888032750432647317316783507777959680594838416103235878202519306235270714
07689264877781232001726371838121263807804664374737613209359850417045192588050394
70657700599839677955302104699239419723871651195923433991338871172082351085772713
85795544605616579911291449220540413076513145849439424174121530477862582225947332
71577283620910275735801384726436002832118799836996365693738642178527175171378406
17487708039971384270639342138853231882316669599224158264716457232654635304792216
85595639961619807370286817974879544351592128186367207420223323272766972084744559
66627509594039497325711708953969586289133165339724320993714241576206582692070672
87281148490183467979899908277488360099016745185705207507885725820344193088663031
74742220273886082811546338253396232117540977001317286430939919216858700761871025
04883648220800173612755757031087544792419044177699287860855715222255510614135023
30706372831160632288499262660942154311052431900104315094339186786507006013357031
08125929293885129152264099174616004246743029176792527824811466605161478045492703
27090040582587712800567514734685257659124144431850835544283278381310315791931357
63459289106249506321560095185194943714045276693796155870508190654607382917421646
15486864936613368395909757833185705018554557948762713677856056206953385065593915
49326215023945424937441813782143778143766220175505533500485707438756395764163323
47714668439588417016028256182365660677880890399143415948539281432076160651247693
72982310275084829961404187585100633245654443972233743905144338234758859827413170
88983027723373315156469586730671906768407758289550985343535503818941377020797426
18604249085624012174154963219214543971133251713032811649089587541472009254502083
21938426925234679535003649815691245256828585977342824147515002509080267191887689
93756807224396178461744274960675671607151017304225081498878127594506757910634207
82366257650930768410626608502254524385838019829644847875689142861722171975269086
27152913039908221672006718321592434013558052896236283594560899204934144043552210
8813052796446077685440711695302241857244755302464656726076289075058391*1337=8446
32735330396026118539488137614033486681484835894553911522401035729672982739951992
59510450846561248771271822738836773072631342658924820547496339499714549977043965
30282959394551897891984120249697406451962655622236761656933974621596222296728397
20183102709193795416428153987614058613458087713533262641419642793700816304805168
68985207407486880443429354748473827987325635935798089498320484478359184559360866
27076583226192391712293067578359927580846468827613754693551137122772118377935650
64416866579070402604224929830477624243882128249086083841362204170859705333927752
53506783887636700979595292482900428172427958399610988487824191523745981361850004
22378394358434742762254571913554781639037404921838294896485533928722618505415775
20468015856906162855718175811402571115916602835577500306211903371964419937747843
68698237621235590531937189523891145666785900034163371124374965082326731487376110
98333333092709828260861059469436406514648183983015665803213913077419107406188372
35707395503957353158240382790401778302613604267141259840425062444532997625763710
49295281541865538168765060653162998657037360956425018150966653699907916926437453
64522455898449750483810932923800543871125123028238779261102286096962295954110422
53300041031861800262087455987369484209627287346984244451204684319717267396169213
64686689162749862681054381010309575068563467271336437198517817577032252061277672
60751210970442593137554100625790557600149517970271550344604758277809500736505629
11439273457043778342886520414406889931665405902225381466032093468840906182958490
40067007538064972385604595988992584428764922065891385936525652108407127397334152
77577987012630748507156907484796296338436704249630828087630933181681675714232538
16498780846317942261636333677867775907817110548388045747416843463971305692382555
89878879946652085040124400663361621041802303798851312902275116583020189066763097
91689409840039779256377571669958449000393642378255399242442113685637190833646231
60095781323920529612304032574006289858849152651185411400724689927483641929978732
84494632525395498991320929552989623300263691567683124365569447208054714159350718
63081591475681297110348362690241888609141200075894224902233777226934570198564942
62389139828831041708163976489496312464200707570741034016781184280864313770936734
13966676078625322832980760007005101208004862489022724360915838409882820115703865
87664513792449357865428353820641409325285645926908332041329290558106564944174076
98448004396467710266573872541626995999259033200592474025071939364137062868568245
40734756324139507980786753851729563208385832156894416773034762738098032723080792
44765548714573368685710420592114171685959409873882010592984896309489553137529668
91261773670019374523853883132878624380432154218001861595424734463034850618569271
90374542447907623411522862507612119581666719929400829186215604902943767120983212
02544471505640473874642620655839478699640912521556176910985261615442047526176536
97235141776796603138771014504394692811314927335598670398583505556436746592441767
65771005964615976778954300093716097017729308511008961468237442731938425891977201
43587672002741894902489811053845671227067431958118922138122251574506953505558995
19258472626056397456785349396054603988694509052100709605927409090593842025207354
53313462229692876098074439774957481872935471486966758326990650144914948701503314
13597050267262313782154363746508982901493908456173011366863634889451170372971355
04297785158228883263267504636547471231970192746858267907181665167734883778841765
83973988012795466494399915908765156011779802198725955892514094797030806615012236
41998374589083393493611728331296674043069686059246210768061588107388102747930427
68451858240898452894051575403248691748327785429480763732692852643167679903876653
82998798035791949083798194517073558852275583546403172355538414465285125901769060
33520956224233729387609101357489339640000565938555353265179358602368647929443736
50077755675142991038654325122351616097963840061225439309347683450344473435729237
54729823959690842761271167222679111659279222369969505862293059483051588848405865
434231536619097363136237839395246042763998493353068767
*and by "eventually", i mean less than 5 seconds on my laptop..
Yes it is a limitation of PHP and there is nothing you can do about it short of recompiling your PHP interpreter. Even then you are limited to the types your native system supports which wouldn't be bigger than 64bit normally. You can, as you know, use GMP, or BCMath, but that is not what your asking.
Under the hood, depending on your system the PHP integer and PHP floating point types corresponds to a signed C integer type, and C float types (PHP always uses C doubles for 'floats' AFAIK). This is a static relationship and can't change after compile time. Since the C types have fixed precision of course the PHP ones do too.
The "overflow" into a float is just a convenient compromise so you can store really big numbers, rather than not at all. Your losing some precision yeah, but only in the significand. PHP is not going to automatically convert the number to some other bigger precision floating point format because it doesn't have one.

Converting two integers into a long in PHP

I've got two integers and I need to convert them into a long. I'm totally lost on how to do this. The two integers that I need to convert are:
INT 1: 60850985
INT 2: 59150141
I need a method that converts two integers into a long. If you can post one, that'd be great.
When I say converting to a long, I'm wondering how you would do the equivalent of (long) from java in PHP
Let me be completely clear here:
I'm receiving two ints from the client, then I need to convert them to a long, then the single long is to be used again as a single int.
In essence, I need to take two ints and convert them into a single int. An example of how this is done in java would be:
long $intA = (long) readInt();
long $intB = (long) readInt();
return ($intA << 32) + $intB;
PHP stores numbers as integers or floats, where the size of the integer is platform dependent (but usally 32 bit, signed). If you need to represent a larger number you should use the BC Math functions.
Using BCM adding two numbers is like:
$a = '60850985';
$b = '59150141';
echo bcadd($a, $b);
or multiplying:
$a = '60850985';
$b = '59150141';
echo bcmul($a, $b);
EDIT:
If you want to get, how it should look as a 64-bit signed integer in 1-complement (if there is an overflow), then you have to do some manual conversion to cast the value inside the -2^63 .. 2^63-1 range:
For nonnegative values you can for example first cast it to 0 .. 2^64-1:
$long = bcmod($value, bcpow('2','64'));
Then if it's larger than 2^63-1 then subtract 2^63 from it, and then subtract this value from -2^63.
For negative values, first add 2^64, until the value is positive, then do the above steps.
But the above steps are only needed if you want to check how the string number looks if there was an overflow. Usually you don't need this, and probably you don't even need to use BCM as your numbers will easily fit inside PHP's signed 32-bit integer datatype (for which no casting is needed, conversion is automatical):
$a = '123'; // $a is a string
echo $a + 1; // will write 124, $a gets converted into int
You can force conversion if you want though:
$a = $a+0;
or
$a = (int)$a;
EDIT 2:
You can still use BCM functions to do the math:
$long = bcadd($low, bcmul($high, bcpow('2','32')))
// this essentially means:
// $long = $low + $high * 2^32
// which is
// $long = $low + $high << 32
Then, if the result is larger than 2^63-1 you can do some additional BCM calculations, described above to convert the unsigned integer into a signed one.
I used regular addition:
$int1 = 60850985;
$int2 = 59150141;
$result = $int1 + $int2;//120001126
Here's a quote from the PHP manual:
The size of an integer is platform-dependent, although a maximum value
of about two billion is the usual value (that's 32 bits signed).
64-bit platforms usually have a maximum value of about 9E18. PHP does
not support unsigned integers. Integer size can be determined using
the constant PHP_INT_SIZE, and maximum value using the constant
PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.
If you want to deal with large numbers use GMP ( http://www.php.net/manual/en/book.gmp.php ) or BCMath ( http://www.php.net/manual/en/book.bc.php ).
To elaborate on what AgentConundrum said, there isn't a need to cast an integer to a long in PHP. PHP is a dynamic language which means that generally, for operations on fundamental types, there is no need for casting (unless it is for some explicit purpose - i.e. a float to an integer), as PHP handles it for you.

Categories