Jan 18, 2011

Not happy with PHP's Int

Few years ago, I used PHP extensively for our core rendering engine...... heh...... I'm kidding...... I used it not for rendering engine;, but for my various personal web projects.  And I recently took another look at this language again.  I wanted to generate some giant random numbers which cannot be represented with normal 32-bit ints.  And this will be stored in MySQL DB.

MySQL supports BIGINT, which is 64-bit, so no problem-o.  The real problem is PHP itself.  PHP's integer is C-like: it can be either 32- or 64-bit.  It depends on which machine the PHP is installed.  (I suspect you also have to install 64-bit PHP distribution to use 64-bit integer)

Sure, this is sorta acceptable on C because you can always build two executables and distribute both.  However, we are talking about PHP here, which is web-based.  Say you make some websites using 64-bit integer on PHP on a 64-bit hosted web server.  Then later you decided to move to another web host but the new one is 32-bit PHP... Then you are f**ked.  What would you do? Refactor all the code with your own custom 64-bit int class? Zawesome.

The other problem is Random() function in PHP, which accepts only an int as a seed and return an int again.  So if you use custom 64-bit int class, you will end up with making two 32-bit random numbers and use one of these as your high 32-bit number.  Sure you can still use floating-point numbers on PHP, but don't expect good precision.... uh.. not my taste......

Don't get me wrong.  I love PHP.  This is definitely my first choice for any web programming language.  But, I really think PHP should support BIGINT just like MySQL...


  1. It's actually worse than the C thing, because in C/C++ you can at least force a 64bit when you need it, leaving open the option to typedef your own int types that are explicit.

  2. Yeah. Also C/C++ requires compiling.. so this type of issue happens while compiling... PHP is scripting.... so this issue happens during runtime.... booo~