title: Why does PHP strlen() return negative length?
body: <p><a href="http://php.net/manual/en/function.strlen.php" rel="nofollow"><code>strlen($str)</code></a> is returning negative values for a "huge string" that is created using <a href="http://php.net/manual/en/function.str-repeat.php" rel="nofollow"><code>str_repeat</code></a>:</p>
  
  <pre><code>&lt;?php
  
  error_reporting(E_STRICT|E_ALL);
  echo phpversion(); // 5.3.26
  echo PHP_INT_MAX; // 9223372036854775807
  ini_set('memory_limit', '-1');
  ini_set('max_execution_time', 0);
  
  $gb = 1024 * 1024 * 1024;
  $str = str_repeat('a', 2 * $gb);
  echo strlen($str); // gives int(-2147483648)
  echo $str[0]; // Notice: Uninitialized string offset: 0
  
  $str2 = str_repeat('a', 4 * $gb);
  echo strlen($str2); // gives int(0)
  
  $str3 = str_repeat('a', 123 + 4 * $gb);
  echo strlen($str3); // gives int(123)
  
  $str4 = str_repeat('a', 6 * $gb); // starts to wrap again...
  echo strlen($str4); // gives int(-2147483648)
  echo $str4[0]; // Notice: Uninitialized string offset: 0
  
  $str5 = str_repeat('a', 123 + 8 * $gb);
  echo strlen($str5); // gives int(123)
  
  ?&gt;
  </code></pre>
  
  <p>Is this behavior defined?</p>
  
  <p>Or is this a PHP bug?</p>