单个字符串变量本身没有架构限制。
编辑:作为用户@Pacerier评论下面,PHP手册现在说:
Note: string can be as large as 2GB.
(当我在2010年第一次回答这个问题时,注意wasn’t in the manual)
但是,PHP脚本在给定脚本执行中为所有变量分配的内存总量有限制,因此这也有效地限制了单个字符串变量的长度。
此限制是php.ini配置文件中的memory_limit伪指令。内存限制在PHP 5.2中默认为128MB,在早期版本中为8MB。
如果你没有在你的php.ini文件中指定一个内存限制,它使用默认值,它被编译成PHP二进制。理论上,您可以修改源并重建PHP以更改此默认值。
如果在php.ini文件中指定-1作为内存限制,它将停止检查并允许您的脚本使用与操作系统将分配的内存一样多的内存。这仍然是一个实际的限制,但取决于系统资源和架构。
从@ c2注释:
这里是一个测试:
-- 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));