Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

The byte and short datatype

...forsaken in PHP

1:39 pm on Jan 9, 2006 (gmt 0)

10+ Year Member

I'm very curious as to why PHP seems to lack so many datatypes. I really need the byte/char datatype, as I am running out of memory storing lots of numbers as integers (which take up 32 bits), intead of bytes (which only take up 8). Surely I can't be the only one to complain about this?

Perhaps a one digit string in PHP only takes up 8 bits, but of course it's impossible to do maths with this, unless you keep using ord() to convert it to a number.

Actually, why can't programming languages in general work with arbitrary datatypes of the form (2^x), where each level x represents the 'next size up'? This way, one could store say... 3 bits to allow for numbers from 0-7, or something like 100 bits for super gigantic numbers. Even C doesn't allow this.

5:24 pm on Jan 9, 2006 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

I don't have an answer to your question but you are correct that PHP supports eight primitive types [php.net] of which four are scalar, including integer.

Yes, you could always cast a single digit as string and PHP will convert it on the fly for you. But it is always best to understand how String conversion to numbers [php.net] work in PHP first.

9:06 am on Jan 11, 2006 (gmt 0)

10+ Year Member

It's worse than I thought. I have 8 megabyte available, but run out by just storing 130000 ints. If an int is 32 bits, that's (32*130000)/8 bytes or 520 kilobytes. Or the memory of an Amiga 500.

So somehow, unless something significant is already eating into that 8 megabyte to begin with (which I doubt), PHP manages to bloat each integer by 16 times. That's actually quite impressive.

Okay, so use chars you say? Well guess what? PHP's one character strings eat up just as much memory as ints! Cool huh?

Sorry, rant over :)

3:23 pm on Jan 11, 2006 (gmt 0)

10+ Year Member

if you're hitting the memory limit of a PHP script, then the obvious solution is to increase the memory limit above 8MB. That's not a fixed limit you know ;)
3:33 pm on Jan 11, 2006 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

True, true.


4:04 pm on Jan 11, 2006 (gmt 0)

10+ Year Member

And the memory limit is PHP_INI_ALL, so you can set it anywhere, including directly in the script with ini_set().

8MB really is not very much memory. It was back when PHP first joined the programming scene, but not anymore. For simple scripts, it is fine, but for other stuff, you really need more. I usually increase my default to about 24MB.

12:06 am on Jan 12, 2006 (gmt 0)

10+ Year Member

Okay thanks all! Yes, this is one way around it. But it's still a kludge, and I'm still surprised at PHP using 64 times as much as it needs to, simply because PHP can't handle a proper char datatype, and also bloats the int datatype to humongous proportions.