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));
Bill Karwin Jul 06 '10 at 18:32 2010-07-06 18:32
source share