0

Is there a way to explicitly store my numbers in php as tinyint (1 byte instead of 4).

Or could i only enforce this by storing them 4 by 4 in an int? (using a few binary operations)

I generate these values by breaking a string using str_split and interpretting these bytes as ints via unpack( 'C' , .. ). Currently i store these values in an array as invdividual integers but it could save alot of space if i could store them somehow as tinyints.

Antiz
  • 1,424
  • 1
  • 13
  • 20
  • TINYINT is a SQL data type- php doesnot have types. However, you can force a type with (int)$x or intval($x) – the Hampster Aug 01 '11 at 22:46
  • I'm a bit confused. The title says "(...) **in** php", yet you talk about storing the data. Do you want to have a tinyint _in_ php, or do you want to save a tinyint to a database _from_ php? –  Aug 01 '11 at 22:49
  • i would like it in php. Say if i could enforce types in php i would make an array and fill it with tinyints there. – Antiz Aug 01 '11 at 22:54
  • I guess i could turn my ints into 1 symbol strings again. But that would make them less accessible and i dont know if 1 symbol strings are actually stored as 1 bit of data internally. – Antiz Aug 01 '11 at 22:56
  • @theHampster You say php has no types but i can force a type onto a variable ? So internally it uses types right? I would just like my ints to be stored internally as 1 byte instead of the current 4 (ints). – Antiz Aug 01 '11 at 23:03
  • @the PHP *has* types. Did you want to say that PHP doesn't have the TINYINT type? – deceze Aug 01 '11 at 23:07

1 Answers1

1

PHP has two data types that you may want to use here: integer and string.
PHP doesn't have any other types you could choose from (float wouldn't be a good choice for integers, the other types are not appropriate).

An int is usually 32 or 64 bits, a string is 1 byte per character.* I propose that unless you have a lot of numbers, you won't ever see any problem with 32 bit ints. If you absolutely positively want to safe space memory** and your numbers have a maximum of 3 digits, you could handle your numbers as strings. There's even the BCMath extension that'll let you operate on string numbers directly without needing to cast them back and forth. It's quite a lot of hassle for possibly very limited gain though.

Seeing that a MySQL TINYINT is usually used for boolean values though, please be aware PHP does have a boolean type...!


* One byte per one-byte character, that is.
** Since PHP scripts are usually only very temporary, you should only have problems with peak memory usage, not storage space. Getting more RAM may be the more efficient solution than playing with types.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • I do have a relativly lot of integers so i just wanted to look at all my options, im guessing my best option is probably to reconsider my current aproach altogether. BCMath seems like too little gain indeed since 255 still needs 3 chars (3 bytes). Yet if i would store them as a 1 character string by packing them, and unpacking when i need them again would that save me 3 bytes per number right? – Antiz Aug 01 '11 at 23:22
  • Come to think about it the keys that my array uses when im storing my integers (or strings) probably take 4 bytes a piece aswell.. Guess you are correct and im just looking for my problem in the wrong place. – Antiz Aug 01 '11 at 23:26
  • @Antiz Yes indeed, `pack`ing and `unpack`ing explicitly into one-byte strings is another way to safe memory, but even more hassle. If you require so much control over number types, PHP probably is the wrong language for whatever you're trying to do. – deceze Aug 01 '11 at 23:40