homepage Welcome to WebmasterWorld Guest from 54.204.249.184
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
The byte and short datatype
...forsaken in PHP
dwhite




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

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.

 

coopster




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

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.

dwhite




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

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 :)

madmac




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

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 ;)

coopster




msg:1260637
 3:33 pm on Jan 11, 2006 (gmt 0)

True, true.

[php.net...]

madmac




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

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.

dwhite




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved