Today, while working with SimpleXML, I noticed some odd behavior while attempting to type-cast an XML attribute. Since the attributes() method of the SimpleXMLElement object will return another SimpleXMLElement object, you must type-cast it in order to save it.
$id = (int) $simplexml->attributes()->Id;
While this worked on my OSX development machine, my co-worker’s Windows 7 Zend Server development environment wasn’t seeing the same result. No matter what we did, we always saw the $id being returned as 2147483647 even though the actual XML was something totally different.
After banging my head against a wall for a bit, I found out that Zend Server for Windows comes with 32-bit PHP. This poses a problem as the IDs we are dealing with are much larger than that. As the PHP documentation notes:
The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that’s 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned integers.
… which meant that our larger IDs were being dropped to the upper limit of the 32-bit integer cap which is 2,147,483,647.
So, if you’re type-casting large integers while using Zend Server, be careful. Even if your OS is 64-bit, your PHP version (as of this post) is 32-bit. You can double check by typing this at the command prompt:
> php -r "echo PHP_INT_MAX;"
On the Zend forums, I found this post detailing why…
We do support all of these we just don’t support 64 bit version of PHP on Windows or Mac. This is intentional because unless you really need a 64 bit integer for some reason the 32 bit version run much much faster than the 64 bit version on these platforms.
I also stumbled on this detailed explanation while trying to figure out what was going on. Hopefully, this post helps cut down debugging time for someone out there as this one can be a bit annoying to nail down.