Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the limit in size of a Php variable when storing string?

This is the case: I have a 2Gb dump file called myDB.sql. It is a dump file that deletes a existing database and creates a new one, with views and triggers. So I have the string myDB_OLD spread for many lines of the code. I would like to change these strings occurrences to myDB_NEW. I could do this easelly using notePad++. But notepad does not opens a 2Gb file. What I did is a PHP code that reads line by line and find and replace the string I want.

This is the code:

$myfile2 = fopen("myDB.sql", "r") or die("Unable to open file!");//Reads the file
while (!feof($myfile2)) {//Pass trough each line
    $str=fgets($myfile2);//Store the line inside a variable
    if (preg_match("/myDB_OLD/",$str)) {//If the string I want to change exists - replace it and conacatenate
        $newStr .= str_replace("myDB_OLD","myDB_NEW",$str);
    } 
    else {//If not concatenate it
        $newStr .=$str;
    }
}//End of while
fclose($myfile2);
//Save the  newStr in a new file named 
$myfile = fopen("newDB.sql", "w") or die("Unable to open file!");
fwrite($myfile,  $newStr);
echo "finished";

This code retrieve each line of the file changes the string, concatenate in variable and creates a new file. It should works but it is not. I dont know why. I am using xdebug to figure out what is the issue, but no luck.

So I change the approach. Instead of save each line in a variable, I save it directly in a file and that works good.

This is the new code:

$myfile = fopen("newDB.sql", "w") or die("Unable to open file!");//Creates a new file "newDB.sql"

$myfile2 = fopen("myDB.sql", "r") or die("Unable to open file!");//Reads the file
while (!feof($myfile2)) {//Pass trough each line
        $str=fgets($myfile2);//Store the line inside a variable
        if (preg_match("/myDB/",$str)) {//If the string I want to change exists - replace it . (Do not concatenate)
            $strRep=str_replace("myDB","myDB_NEW",$str);
        }
        else {
            $strRep =$str;
        }

        fwrite($myfile,  $strRep);// Add the new line to the file "newDB.sql"
}//End of while
fclose($myfile);
fclose($myfile2);

echo "finished";

Ok, I solved my issue but it raises a thought. What is the issue of the first code? I think the issue is the amount of information to be stored in a PHP variable, 2Gb. So, is there a limit in size to a PHP variable to stores value, in this case, a string ? If yes how can I check or change it? Any php.ini variable?

like image 877
IgorAlves Avatar asked Nov 23 '15 15:11

IgorAlves


People also ask

What is the maximum size of string in PHP?

A string is series of characters, where a character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support.


2 Answers

So, is there a limit in size to a Php variable to stores value, in this case, a string ?

Yes. A string can be as large as up to 2GB (2147483647 bytes maximum). You can override this limit by increasing memory_limit directive in php.ini.

From php7 there's not that limitation in a 64 bit system:

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

like image 144
Federkun Avatar answered Oct 26 '22 13:10

Federkun


The maximum number of bytes a script is allowed to allocate is set in php.ini. Look for memory_limit. This is 16Mb after PHP 5.2.0 as a default!

This can be changed by doing a:

ini_set('memory_limit','24M');

The maximum size of a string is 2 GB in PHP, probably because of adressing limitations, if allowed by memory_limit.

like image 25
Hasse Björk Avatar answered Oct 26 '22 12:10

Hasse Björk