note 98114 added to function.gmp-strval

From: Date: Wed, 26 May 2010 17:22:19 +0000
Subject: note 98114 added to function.gmp-strval
Groups: php.notes 
Request: Send a blank email to php-notes+get-169627@lists.php.net to get a copy of this message
A gotcha when using the value from gmp_strval as a key in an associative array. Some numbers are 2^31 are converted to signed integer format, this is usually incorrect. <?php $a = gmp_init('2147483649'); // = 2^31 + 1 $b = gmp_init('3173604585'); // = 2^31 < $b < 2^32 $c = gmp_strval($a); $d = gmp_strval($b); $e = array($c => 'c', $d => 'd'); print '*** Expect '."\n"; print 'Array '."\n".'('."\n".' ['.$c.'] => c'."\n".' ['.$d.'] => d'."\n".')'."\n\n"; print '*** Actual '."\n"; print_r($e); ?> Output: *** Expect Array ( [2147483649] => c [3173604585] => d ) *** Actual Array ( [-2147483647] => c [3173604585] => d ) A workaround is to format as base 16 and append '0x' before using as a key. ---- Server IP: 69.147.83.197 Probable Submitter: 199.243.246.66 ---- Manual Page -- http://www.php.net/manual/en/function.gmp-strval.php Edit -- https://master.php.net/note/edit/98114 Del: integrated -- https://master.php.net/note/delete/98114/integrated Del: useless -- https://master.php.net/note/delete/98114/useless Del: bad code -- https://master.php.net/note/delete/98114/bad+code Del: spam -- https://master.php.net/note/delete/98114/spam Del: non-english -- https://master.php.net/note/delete/98114/non-english Del: in docs -- https://master.php.net/note/delete/98114/in+docs Del: other reasons-- https://master.php.net/note/delete/98114 Reject -- https://master.php.net/note/reject/98114 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#169627) next »