What is the maximum string length in PHP?

So how big can $variable in PHP be? I tried to verify this, but I'm not sure that I have enough system memory (~ 2gb). I suppose there should be some limit. What happens when a row gets too big? Is it concatenated, or is PHP throwing an exception?

+58
php
Jul 06 '10 at 18:20
source share
7 answers

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

Note. With PHP 7.0.0, there are no special restrictions on the length of the string in 64-bit assemblies. In 32-bit builds and in earlier versions, the string can reach 2 GB (maximum 2147483647 bytes)

In PHP 5.x, strings were limited to 2 31 -1 bytes, because the internal code wrote the length to a 32-bit integer.




You can smooth out the contents of the entire file, for example using file_get_contents()

However, the PHP script has a limit on the total memory that it can allocate for all variables in a given script execution, so this also limits the length of one string variable.

This limit is the memory_limit directive in the php.ini configuration file. The default memory limit is 128 MB in PHP 5.2 and 8 MB in earlier versions.

If you did not specify a memory limit in the php.ini file, it uses the default value that is compiled into the PHP binary. In theory, you can change the source and rebuild PHP to change this default value.

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




Re comment from @ c2:

Here's the 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)); 
+87
Jul 06 '10 at 18:32
source share

The string can be like 2 GB.
Source

+16
May 20 '13 at 2:58
source share

PHP string length is limited by how the string is represented in PHP; memory has nothing to do with it.

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

+4
Apr 05 '14 at 2:34
source share

The maximum length of a string variable is only 2GiB - (2 ^ (32-1) bits). Variables can be addressed based on a character (8 bits / 1 byte), and addressing is done in whole characters, so the limit is what it is. Arrays can contain several variables, each of which corresponds to the previous restriction, but can have a total cumulative size up to memory_limit, for which a string variable is also subordinate.

+2
Sep 25 '14 at 18:48
source share

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

Support for strings with a length> = 2 ^ 31 bytes in 64-bit assemblies.

Unfortunately, they did not indicate how much more this could be.

+2
Jun 27. '15 at 4:14
source share

In order to correctly answer this question, you need to consider the internals of PHP or the purpose for which PHP is built.

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

Type sizes 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

The strings are always 2 GB, because the length is always 32 bits, and the bit is wasted because it uses int, not uint. int is not practical for lengths greater than 2 GB, since it requires a throw to avoid arithmetic or "comparative" comparisons. The extra bit is probably used to check for overflow.

Strangely, hash keys can internally support 4 GB, as uint is used, although I never applied this to the test. PHP hash keys have +1 to the length for the final null byte, which, as far as I know, is ignored, so for this case, the root may be required unsigned, and not to provide longer keys.

A 32-bit system may impose more external constraints.

0
Oct 22 '15 at 20:21
source share

No restrictions.

-7
Jul 06 '10 at 18:22
source share



All Articles