0

Edit: I have made a demo to my problem: http://codepad.org/ByNdAdCI

We use the following logic to set our arrays in 32 bit php:

    private function formatAvailableOptions($availableOptions) {
    $optionsAsArray = array();
    foreach($availableOptions as $option){
        $optionsAsArray[$option["entity_id"]] = $option["name"];
    }
    return $optionsAsArray;
}

Problem:

  1. The $option["entity_id"] has grown too big to fit in 32 bit int, and as a result overflow when $optionsAsArray gets created. For example

    $optionsAsArray[2147483648] = "hi";

becomes

$optionsAsArray[-2147483648] = "hi";

I need the index to be 2147483648 as it's tied to ids in the database

  1. This code is already shipped to the client, and the logic is used in many places that makes it infeasible to modify every single instance to

    $optionsAsArray[strVal(2147483648)] = "hi";

to make it run properly by using string type as key to associative array.

What is the best solution in this case? Is there a global config option for me to force all php arrays to use string as keys to get around this problem?

  • 2
    Your problem must be somewhere else http://codepad.org/WReVhjTF PHP automatically converts `int` to `float`, if they exceed their limits. Probably the values already came in as `-1`. However, if they overflow, they will _not_ become `-1`, but they will become `- PHP_INT_MAX + 1`, what is just another evidence, that your problem must be somewhere else. Therefore this is a (maybe important) bug and with this in mind you should consider to ship an update to your costumers. – KingCrunch Jun 14 '12 at 17:48
  • Sorry you're right, I was confused. I played with code on codepad to better demonstrate my overflow issue: http://codepad.org/ByNdAdCI – Tsung-Ting Chen Jun 14 '12 at 17:56
  • I can duplicate your problem on an old Ubuntu 8.x server running PHP 5.2.4 32-bit, but not on anything with PHP 5.3 or higher running 64-bit. In what form does your input data arrive? Perhaps you could preprocess it with `sed` to put quotes around your keys? – ghoti Jun 14 '12 at 18:09
  • @ghoti Of course you can't reproduce it on 64bit systems: `echo PHP_INT_MAX`. – KingCrunch Jun 14 '12 at 18:12
  • I can pre-cast the keys to string and that would fix the issue, I was hoping not having to do that as a lot of code duplicates this stupid logic. But from the suggestions I'm seeing I would either have to 1. Upgrade all clients to 64 bits or 2. Roll fix out for dozens of files : P – Tsung-Ting Chen Jun 14 '12 at 18:29
  • 2
    @KingCrunch - of course you can't. But sometimes, explaining things in more detail with real-world examples helps people understand. Chill, dude, and don't be so patronizing. It doesn't help, and it doesn't encourage others to help. – ghoti Jun 14 '12 at 19:02
  • @Rosemary Thyme upgrading all clients to 64 bits will solve not only this, but also other potential issues that may arise from the mismatch of integer datatypes between the DB and PHP – Walter Tross Jun 14 '12 at 20:05
  • @ghoti Oh, I'm calm :) But the 64bit-thing was just too obvious ^^ – KingCrunch Jun 14 '12 at 20:28

1 Answers1

1

Seems, that it doesn't like floats as keys ;) So the type cast to float fails. You already mentioned your solution in the question (strVal($var), or (string) $var, or just "$var"). If your customers run PHP in 64bit (what they should ;)) they don't see any difference. Because this is obviously a bug you should consider to fix it and publish an update to your customers. There is no option, or setting, that converts a 32bit installation into a 64bit one on the fly.

nickb
  • 59,313
  • 13
  • 108
  • 143
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • I am investigating upgrading to 64-bit php, however on the windows platform, it seems that the underlying compiler everyone uses (Visual C++ 9) doesn't use 8 byte ints. A comment on this question:http://stackoverflow.com/questions/864058/how-to-have-64-bit-integer-on-php suggests that having 64 bit ints on windows is impossible – Tsung-Ting Chen Jun 18 '12 at 15:55
  • Must say: Don't know. I only develop on linux, because less difficulties (like this one ;)) – KingCrunch Jun 18 '12 at 21:04