77

So how big can a $variable in PHP get? I've tried to test this, but I'm not sure that I have enough system memory (~2gb). I figure there has to be some kind of limit. What happens when a string gets too large? Is it concatenated, or does PHP throw an exception?

rook
  • 66,304
  • 38
  • 162
  • 239

6 Answers6

109

http://php.net/manual/en/language.types.string.php says:

Note: As of PHP 7.0.0, there are no particular restrictions regarding the length of a string on 64-bit builds. On 32-bit builds and in earlier versions, a string can be as large as up to 2GB (2147483647 bytes maximum)

In PHP 5.x, strings were limited to 231-1 bytes, because internal code recorded the length in a signed 32-bit integer.


You can slurp in the contents of an entire file, for instance using file_get_contents()

However, a PHP script has a limit on the total memory it can allocate for all variables in a given script execution, so this effectively places a limit on the length of a single string variable too.

This limit is the memory_limit directive in the php.ini configuration file. The memory limit defaults to 128MB in PHP 5.2, and 8MB in earlier releases.

If you don't specify a memory limit in your php.ini file, it uses the default, which is compiled into the PHP binary. In theory you can modify the source and rebuild PHP to change this default value.

If you specify -1 as the memory limit in your php.ini file, it stop checking and permits your script to use as much memory as the operating system will allocate. This is still a practical limit, but depends on system resources and architecture.


Re comment from @c2:

Here's a test:

<?php

// limit memory usage to 1MB 
ini_set('memory_limit', 1024*1024);

// initially, PHP seems to allocate 768KB for basic operation
printf("memory: %d\n",  memory_get_usage(true));

$str = str_repeat('a',  255*1024);
echo "Allocated string of 255KB\n";

// now we have allocated all of the 1MB of memory allowed
printf("memory: %d\n",  memory_get_usage(true));

// going over the limit causes a fatal error, so no output follows
$str = str_repeat('a',  256*1024);
echo "Allocated string of 256KB\n";
printf("memory: %d\n",  memory_get_usage(true));
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • So what's the best way to work within the memory limit if we really need long strings? – Pacerier Jun 06 '13 at 14:47
  • When PHP.net states "Note string can be as large as 2GB." http://php.net/manual/en/language.types.string.php do they mean it can go *over* 2GB? – Pacerier Jun 06 '13 at 14:49
  • @Pacerier, good catch! That note was not in the manual page when I first answered this question in 2010. I will edit my answer above. – Bill Karwin Jun 06 '13 at 16:26
  • 1
    Please see my response below, the limit is imposed by how strings are represented in PHP, not by memory limit. – c 2 Apr 05 '14 at 14:36
  • @c2, +1 for going to the source, but see my demo of memory limit above. – Bill Karwin Apr 05 '14 at 16:15
  • @BillKarwin, I am still not sure if that's the "correct" answer. I agree that the size of a string varies by PHP memory config, but the hard limit for maximum size of PHP string ultimately constrained by it's internal structure? – c 2 Apr 05 '14 at 17:20
  • @c2, well I thought I addressed that at the top of my answer, when I edited it after Pacerier's comment. The manual does state that the string length limit is 2GB, but in a given PHP installation, it's likely that the `memory_limit` is configured to be a lot smaller than that. – Bill Karwin Apr 05 '14 at 21:29
  • @BillKarwin, New update: manual now states "[2147483647 bytes maximum](http://stackoverflow.com/a/17576605/632951)" (as opposed to implicit 2147483648). – Pacerier Jan 14 '16 at 02:31
  • I did this today with `ini_set('memory_limit', 1024*1024*1024*2);` and it worked beautifully. – jpgerb Oct 27 '22 at 19:42
17

String can be as large as 2GB.
Source

Touhid Rahman
  • 2,455
  • 21
  • 22
4

PHP's string length is limited by the way strings are represented in PHP; memory does not have anything to do with it.

According to phpinternalsbook.com, strings are stored in struct { char *val; int len; } and since the maximum size of an int in C is 4 bytes, this effectively limits the maximum string size to 2GB.

c 2
  • 1,127
  • 3
  • 13
  • 21
  • 1
    .. why the f wouldn't they use unsigned int here? not like a string can ever be LESS THAN 0 BYTES LONG :p – hanshenrik Jul 02 '15 at 16:23
  • 2
    The is not entirely correct. 2GB is 31bits. They waste a bit by using a signed datatype. Perhaps this was to simplify things such as overflow checks without requiring a larger value or specif(concat A + B, uint total = A.len + B.len, if total > TYPE_MAX/2 then error), I don't think C allows an overflow check without dropping into assembly. – jgmjgm Oct 22 '15 at 20:00
  • @jgmjgm 2GB in 31bits it's because the 32th bit is for the sign. If it'd been "unsinged int len" then you'd have 32bits and 4GB – Jack Feb 10 '16 at 15:08
3

In a new upcoming php7 among many other features, they added a support for strings bigger than 2^31 bytes:

Support for strings with length >= 2^31 bytes in 64 bit builds.

Sadly they did not specify how much bigger can it be.

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • Perhaps 2^63 - 1? I don't even know if that much RAM is available somewhere ... . – hakre Aug 27 '15 at 17:10
  • In theory it could allow 2^32 or 2^64. For whatever reason, signed ints or longs are used. Because this would break code in many places (if len < something) in the rare case a string length ever did overflow. – jgmjgm Oct 22 '15 at 19:56
  • i bet its 0x7FFFFFFFFFFFFFFF or 9223372036854775807 bytes (or 9.2 exabyts), minus whatever PHP is already using, minus whatever ASLR is wasting, because, you know, the 64bit virtual memory space would be exhausted :D – hanshenrik Jun 18 '16 at 10:11
2

The maximum length of a string variable is only 2GiB - (2^(32-1) bits). Variables can be addressed on a character (8 bits/1 byte) basis and the addressing is done by signed integers which is why the limit is what it is. Arrays can contain multiple variables that each follow the previous restriction but can have a total cumulative size up to memory_limit of which a string variable is also subject to.

jgmjgm
  • 4,240
  • 1
  • 25
  • 18
dsx724
  • 31
  • 2
0

To properly answer this qustion you need to consider PHP internals or the target that PHP is built for.

To answer this from a typical Linux perspective on x86...

Sizes of types in C: https://usrmisc.wordpress.com/2012/12/27/integer-sizes-in-c-on-32-bit-and-64-bit-linux/

Types used in PHP for variables: http://php.net/manual/en/internals2.variables.intro.php

Strings are always 2GB as the length is always 32bits and a bit is wasted because it uses int rather than uint. int is impractical for lengths over 2GB as it requires a cast to avoid breaking arithmetic or "than" comparisons. The extra bit is likely being used for overflow checks.

Strangely, hash keys might internally support 4GB as uint is used although I have never put this to the test. PHP hash keys have a +1 to the length for a trailing null byte which to my knowledge gets ignored so it may need to be unsigned for that edge case rather than to allow longer keys.

A 32bit system may impose more external limits.

jgmjgm
  • 4,240
  • 1
  • 25
  • 18