2

I have a very long string stored in $str. Now I do $sub = substr($str, $pos) and I will do only reading operations on both strings.

Is the long string still stored in memory only once, or is it split by the moment of substr call? Is there any way to test it on PHP level?

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
  • 1
    Have a look at [how-efficient-is-phps-substr](https://stackoverflow.com/questions/2813119/how-efficient-is-phps-substr), I believe this post has the same intent. It also provides an answer on how to use strpos to get yourself a more memory efficient solution for read only substrings. – Remy Feb 06 '21 at 12:02

3 Answers3

0

An example:

<?php
    $originalstr = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse et nisl in diam fermentum convallis et sit amet nulla. In at massa ipsum. Vestibulum tempus egestas elit, sollicitudin ullamcorper mauris dictum in. Etiam non mollis velit. Vestibulum tincidunt urna ut posuere placerat. Ut cursus ipsum non ipsum auctor, vitae accumsan sem pretium. Praesent porta vestibulum nisl, a malesuada nisi finibus at. Morbi aliquet vitae ante et porttitor. Donec laoreet nisi ut neque luctus vulputate. Donec sollicitudin massa at turpis lobortis laoreet. Etiam porta lacinia placerat. Aenean venenatis dui quis elit facilisis porta. Donec vel semper odio. Vivamus mauris neque, egestas ac mi ac, rutrum dignissim dui. Fusce consectetur eleifend luctus. Vestibulum accumsan rutrum magna iaculis tempus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec eget arcu sodales, porttitor magna nec, scelerisque purus.";  // the string
    $substr = substr($originalstr, 0, 50);
    echo $substr. "<br><hr><br>". $originalstr;  //prints the first 50 characters; and then prints the actual string
?>

Output:

Lorem ipsum dolor sit amet, consectetur adipiscing


Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse et nisl in diam fermentum convallis et sit amet nulla. In at massa ipsum. Vestibulum tempus egestas elit, sollicitudin ullamcorper mauris dictum in. Etiam non mollis velit. Vestibulum tincidunt urna ut posuere placerat. Ut cursus ipsum non ipsum auctor, vitae accumsan sem pretium. Praesent porta vestibulum nisl, a malesuada nisi finibus at. Morbi aliquet vitae ante et porttitor. Donec laoreet nisi ut neque luctus vulputate. Donec sollicitudin massa at turpis lobortis laoreet. Etiam porta lacinia placerat. Aenean venenatis dui quis elit facilisis porta. Donec vel semper odio. Vivamus mauris neque, egestas ac mi ac, rutrum dignissim dui. Fusce consectetur eleifend luctus. Vestibulum accumsan rutrum magna iaculis tempus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec eget arcu sodales, porttitor magna nec, scelerisque purus.

In the PHP Manual:

substr

(PHP 4, PHP 5, PHP 7, PHP 8)

substr — Return part of a string Description ¶ substr ( string $string , int $offset , int|null $length = null ) : string

Returns the portion of string specified by the offset and length parameters. Parameters ¶

string

The input string. offset

If offset is non-negative, the returned string will start at the offset'th >position in string, counting from zero. For instance, in the string 'abcdef', the >character at position 0 is 'a', the character at position 2 is 'c', and so forth.

If offset is negative, the returned string will start at the offset'th >character from the end of string.

If string is less than offset characters long, false will be returned.

Example #1 Using a negative offset

   <?php
   $rest = substr("abcdef", -1);    // returns "f"
   $rest = substr("abcdef", -2);    // returns "ef"
   $rest = substr("abcdef", -3, 1); // returns "d"
   ?>

length

If length is given and is positive, the string returned will contain at most >length characters beginning from offset (depending on the length of string).

If length is given and is negative, then that many characters will be omitted >from the end of string (after the start position has been calculated when a >offset is negative). If offset denotes the position of this truncation or beyond, >false will be returned.

If length is given and is 0, false or null, an empty string will be returned.

If length is omitted, the substring starting from offset until the end of the >string will be returned.

Example #2 Using a negative length

   <?php
   $rest = substr("abcdef", 0, -1);  // returns "abcde"
   $rest = substr("abcdef", 2, -1);  // returns "cde"
   $rest = substr("abcdef", 4, -4);  // returns false
   $rest = substr("abcdef", -3, -1); // returns "de"
   ?>

Return Values ¶

Returns the extracted part of string; or false on failure, or an empty >string.

I do not think they have mentioned anything about memory at all. So, you would need to test the memory usage spike when running the PHP app.

They have mentioned: If length is given and is positive, the string returned will contain at most >length characters beginning from offset (depending on the length of string).

But due to common sense, they have probably designed it to only take the first number of characters from that variable, and then just stop doing it when the $pos is reached.

Have fun cod3ing!

Example person
  • 3,198
  • 3
  • 18
  • 45
0

For the PHP level: Here is an example you can test it on any online PHP compiler.

The example will show you that the string will be the same without changing and only the sub string will be the new string (0 : start index, 5 : end index).

If you have another question or unclear inquiry feel free to ask me again.

  <?php
      $txt = "test_string_in_php_demo";  //string
      $sub = substr($txt, 0, 5);
      echo $sub;  //prints the sub string from start to index 5 
      echo "<br>"; 
      echo "<br>";
      echo $txt;  // prints the full txt;
    ?>

OutPut:

test_

test_string_in_php_demo

Extra Resources:
Please Check this question Too.
Handling large strings in PHP.

Mohamed Bdr
  • 967
  • 3
  • 9
  • 20
  • 2
    The question is about the memory: if the `$sub` address is `$txt` address plus 5, the memory is shared. It is easy to find this out in C, but I have no clue what PHP does behind the curtain. – Jan Turoň Feb 06 '21 at 11:48
0

I just found memory_get_usage function:

echo memory_get_usage()."<br>";       // 391544
$str = file_get_contents("data.bin"); // 500 kb
echo memory_get_usage()."<br>";       // 961000
$sub = substr($str, 100000);
echo memory_get_usage()."<br>";       // 1432040
unset($sub);
echo memory_get_usage()."<br>";       // 961000
unset($str);
echo memory_get_usage()."<br>";       // 391656

As seen from the test, PHP does not optimize here memory management at all! Very sad.

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169