1

For the last few days I have been working on a lot of socket-related tasks and had to make use of the pack() and unpack() functions.

For those of you, who have never used these functions: the pack function packs a number of variables into a binary string. For example you could store 8 boolean variables on just one byte this way.

Now, while unpacking my data I noticed a rather peculiar behavior. Let’s see:

php > var_dump(unpack('n2', pack('n2', 1234, 1234)));
array(2) {
  [1]=>
  int(1234)
  [2]=>
  int(1234)
}

As you can see, the result array of unpack is indexed from 1 instead of the usual 0. (Most PHP functions return arrays indexed from 0.)

My question is, is there a specific reason PHP does this, or is it a bug?

4

0 回答 0