php PHP中字符串的最大长度是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3189040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the maximum length of a String in PHP?
提问by rook
So how big can a $variablein 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?
那么$variablePHP 中的a 可以有多大呢?我试图对此进行测试,但我不确定我是否有足够的系统内存 (~2gb)。我认为必须有某种限制。当字符串变得太大时会发生什么?它是串联的,还是 PHP 抛出异常?
回答by Bill Karwin
http://php.net/manual/en/language.types.string.phpsays:
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.x 中,字符串被限制为 2 31-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_limitdirective in the php.ini configuration file. The memory limit defaults to 128MB in PHP 5.2, and 8MB in earlier releases.
此限制是memory_limitphp.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 -1as 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.
如果您-1在 php.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));
回答by c 2
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; 内里;并且由于 C 中 int 的最大大小为 4 个字节,因此这有效地将最大字符串大小限制为 2GB。
回答by dsx724
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.
字符串变量的最大长度仅为 2GiB -(2^(32-1) 位)。变量可以以字符(8 位/1 字节)为基础进行寻址,并且寻址是通过有符号整数完成的,这就是限制就是它的原因。数组可以包含多个变量,每个变量都遵循前面的限制,但总累积大小可以达到 memory_limit,其中一个字符串变量也受到限制。
回答by Salvador Dali
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.
在 64 位构建中支持长度 >= 2^31 字节的字符串。
Sadly they did not specify how much bigger can it be.
可悲的是,他们没有具体说明它可以大多少。
回答by jgmjgm
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/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
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。int 对于超过 2GB 的长度是不切实际的,因为它需要强制转换以避免破坏算术或“比”比较。额外的位可能用于溢出检查。
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 位系统可能会施加更多外部限制。

