The Zend_Amf specification states that a Number type returned from flash will map to a float in PHP. Fine. But why does the number 16 get returned as 6.1026988574311E_320 ? PHP version is 5.2.9 running on OS X.

I have tried forcing a cast to integer in PHP (the above value gets rounded to 0) and also from Actionscript using int(16) - the latter comes through as NULL. How can ensure that Flash returns an integer via AMF and that PHP can deal with it?

+1  A: 
Heath Hunnicutt
+5  A: 

You have a classic endian problem. Looks like either Zend or flash is doing the wrong thing with the endianness of this double. Here is the a program that prints the double (and its hex value). It then reverses the endianness and prints it again.

#include <stdio.h>
#include <stdint.h>

int main(void)
  double d = 16;
  uint32_t *i = (uint32_t *)(&d);
  uint8_t *c = (uint8_t *)(&d);
  size_t j;

  printf("%08x %08x %lg\n", i[1], i[0], d);

  for(j = 0; j < sizeof(double) / 2; j++)
     uint8_t tmp;

     tmp = c[j];
     c[j] = c[sizeof(double) - j - 1];
     c[sizeof(double) - j - 1] = tmp;

  printf("%08x %08x %lg\n", i[1], i[0], d);

  return 0;

On an Intel (little endian processor), you get this output

40300000 00000000 16
00000000 00003040 6.1027e-320

Are you perhaps running this on a PPC Mac (big endian)? Seems like one of your tools is not doing the right thing on your architecture. File a bug with the vendors.

As a hacky workaround, I suggest turning your number into a string and then converting it back to a double on the other end.

Variable Length Coder
It was running on a PPC mac, yes but it also happened on a Windows machine. I'm not sure of the exact architecture and no longer have access to either machine to test unfortunately. Thanks for your answer though! It sounds plausible.