PHP中字符串的最大长度是多少?

时间:2022-12-08 21:42:43

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?

那么PHP中的$变量能有多大呢?我尝试过测试它,但是我不确定我有足够的系统内存(~2gb)。我认为一定有某种限制。当字符串变得太大时会发生什么?它是连接的,还是PHP抛出异常?

6 个解决方案

#1


93  

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

http://php.net/manual/en/language.types.string.php表示:

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)

注意:对于PHP 7.0.0,对于64位构建中的字符串长度没有特定的限制。在32位的构建和早期版本中,一个字符串最多可达2GB(最大2147483647字节)

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

在PHP 5。字符串被限制为231-1字节,因为内部代码以带符号的32位整数记录长度。


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

您可以提取整个文件的内容,例如使用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.

但是,PHP脚本对在给定脚本执行中可以分配给所有变量的总内存有限制,因此这也有效地限制了单个字符串变量的长度。

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.

这个限制是php中的memory_limit指令。ini配置文件。在PHP 5.2中,内存限制默认为128MB,在早期版本中为8MB。

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.

如果没有在php中指定内存限制。它使用默认的ini文件,它被编译成PHP二进制文件。理论上,您可以修改源代码并重新构建PHP来更改这个默认值。

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.

如果在php中指定-1作为内存限制。ini文件,它停止检查并允许您的脚本使用操作系统分配的内存。这仍然是一个实际的限制,但是依赖于系统资源和体系结构。


Re comment from @c2:

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

#2


16  

String can be as large as 2GB.
Source

字符串可以大到2GB。源

#3


4  

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

PHP的字符串长度受PHP中字符串表示方式的限制;记忆和它没有任何关系。

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.

根据phpinternalsbook.com,字符串存储在struct {char *val;int len;由于C中int的最大大小为4字节,所以这实际上限制了最大字符串大小为2GB。

#4


2  

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

在一个新的即将php7许多其他功能,他们增加了一个支持字符串比2 ^ 31个字节:

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

支持字符串长度> = 2 ^ 31个字节64位构建。

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

遗憾的是,他们没有具体说明它能有多大。

#5


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.

字符串的最大长度变量只有2直布罗陀海峡-(2 ^(32-1)比特)。变量可以在一个字符(8位/1字节)的基础上处理,而寻址是通过有符号整数完成的,这就是为什么限制是它是什么。数组可以包含多个变量,每个变量都遵循前面的限制,但是可以有一个总的累积大小,直到一个字符串变量也受memory_limit的限制。

#6


0  

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

要正确地回答这个问题,您需要考虑PHP内部内容或PHP构建的目标。

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

从x86上的典型Linux视角来回答这个问题……

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

C中的类型大小:https://usrmisc.wordpress.com/2012/12/27/inte-sizs-in -on-32位-64位-linux/

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

PHP中用于变量的类型: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.

字符串总是2GB,因为长度总是32位,并且有一点被浪费了,因为它使用int而不是uint。对于超过2GB的长度来说,int是不切实际的,因为它需要一个cast来避免破坏算术或“than”比较。额外的位可能用于溢出检查。

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.

奇怪的是,哈希键可能内部支持4GB的uint,尽管我从来没有把它放在测试中。PHP哈希键的长度为+1,而我的知识被忽略了,因此它可能需要在这个边界情况下没有签名,而不是允许更长的键。

A 32bit system may impose more external limits.

一个32位的系统可能会施加更多的外部限制。

#1


93  

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

http://php.net/manual/en/language.types.string.php表示:

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)

注意:对于PHP 7.0.0,对于64位构建中的字符串长度没有特定的限制。在32位的构建和早期版本中,一个字符串最多可达2GB(最大2147483647字节)

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

在PHP 5。字符串被限制为231-1字节,因为内部代码以带符号的32位整数记录长度。


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

您可以提取整个文件的内容,例如使用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.

但是,PHP脚本对在给定脚本执行中可以分配给所有变量的总内存有限制,因此这也有效地限制了单个字符串变量的长度。

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.

这个限制是php中的memory_limit指令。ini配置文件。在PHP 5.2中,内存限制默认为128MB,在早期版本中为8MB。

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.

如果没有在php中指定内存限制。它使用默认的ini文件,它被编译成PHP二进制文件。理论上,您可以修改源代码并重新构建PHP来更改这个默认值。

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.

如果在php中指定-1作为内存限制。ini文件,它停止检查并允许您的脚本使用操作系统分配的内存。这仍然是一个实际的限制,但是依赖于系统资源和体系结构。


Re comment from @c2:

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

#2


16  

String can be as large as 2GB.
Source

字符串可以大到2GB。源

#3


4  

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

PHP的字符串长度受PHP中字符串表示方式的限制;记忆和它没有任何关系。

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.

根据phpinternalsbook.com,字符串存储在struct {char *val;int len;由于C中int的最大大小为4字节,所以这实际上限制了最大字符串大小为2GB。

#4


2  

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

在一个新的即将php7许多其他功能,他们增加了一个支持字符串比2 ^ 31个字节:

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

支持字符串长度> = 2 ^ 31个字节64位构建。

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

遗憾的是,他们没有具体说明它能有多大。

#5


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.

字符串的最大长度变量只有2直布罗陀海峡-(2 ^(32-1)比特)。变量可以在一个字符(8位/1字节)的基础上处理,而寻址是通过有符号整数完成的,这就是为什么限制是它是什么。数组可以包含多个变量,每个变量都遵循前面的限制,但是可以有一个总的累积大小,直到一个字符串变量也受memory_limit的限制。

#6


0  

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

要正确地回答这个问题,您需要考虑PHP内部内容或PHP构建的目标。

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

从x86上的典型Linux视角来回答这个问题……

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

C中的类型大小:https://usrmisc.wordpress.com/2012/12/27/inte-sizs-in -on-32位-64位-linux/

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

PHP中用于变量的类型: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.

字符串总是2GB,因为长度总是32位,并且有一点被浪费了,因为它使用int而不是uint。对于超过2GB的长度来说,int是不切实际的,因为它需要一个cast来避免破坏算术或“than”比较。额外的位可能用于溢出检查。

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.

奇怪的是,哈希键可能内部支持4GB的uint,尽管我从来没有把它放在测试中。PHP哈希键的长度为+1,而我的知识被忽略了,因此它可能需要在这个边界情况下没有签名,而不是允许更长的键。

A 32bit system may impose more external limits.

一个32位的系统可能会施加更多的外部限制。