|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2000-08-24 18:40 UTC] aaron at meta dot lo-res dot org
type in : <? $str = "foobar"; $val = ord($str[0]); echo "val = $val <hr>"; ?> -> the apache process serving the page crashes PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 11 15:00:01 2026 UTC |
Sorry, again a correction: the problem was NOT the ord() function, but rather that PHP will not cope with large integers (even below 2^31). Maybe the problem was the modulo operator and large numbers. I will reprint my original source here: function hash($str) { // $p = 4294967291; /* largest prime <= 2^32 */ // $p = 2147483647; /* largest prime <= 2^31 */ // $p = 1073741789; /* largest prime <= 2^30 */ // $p = 268435399; /* largest prime <= 2^28 */ $p = 33554393 ; /* largest prime <= 2^25 */ // $p = 65521; /* largest prime <= sqrt(2^32) */ $BASE = 256; /* largest char */ if ($str == -1) { return -1; } $len = strlen($str); $hash = ord($str[0]); for ($i = 1; $i < $len; $i++) { debug("i = $i"); $hash = (($hash * 32) + ord($str[$i])) % $p; debug("hash = $hash"); } return $hash; } ---- you can see from the values for $p some kind of boundary where the arithmetic ops wont work anymore. The debug() function basically echos the given string parameter. so nothing special about that. Unfortunately I cannot change the name of the PR anymore but still please keep me informed what I can do to work with large numbers. thanks, aaron.