title: php-ffi: Return type const char* is a string
body: <p>Why is the result of <code>const char *</code> a <code>string</code> and <code>char *</code> an object „byte-array“?</p>
  <p>A simple example:</p>
  <pre class="lang-php prettyprint-override"><code>$ffi = FFI::cdef(
    &quot;const char *strerror(int errnum);&quot;,
    &quot;libc.so.6&quot;
  );
  var_dump($ffi-&gt;strerror(1));
  </code></pre>
  <p>returns</p>
  <pre class="lang-php prettyprint-override"><code>string(23) &quot;Operation not permitted&quot;
  </code></pre>
  <p>Removing <code>const</code> from the definition returns:</p>
  <pre class="lang-php prettyprint-override"><code>object(FFI\CData:char*)#1074 (1) {
    [0]=&gt;
    string(1) &quot;O&quot;
  }
  </code></pre>
  <p><strong>Background</strong></p>
  <p>A function of a dynamically linked external C library returns binary data which include NULL-bytes. When <code>const</code> is in the definition of the header file the resulting string is not complete = shortened to the first NULL-byte = corrupt file.</p>
  <p><strong>My workaround</strong></p>
  <p>Remove <code>const</code> from the definition and</p>
  <pre class="lang-php prettyprint-override"><code>$array = $ffi-&gt;strerror(1);
  $string = FFI::string($array);
  </code></pre>
  <p>returns the correct complete string.</p>
  <p>(In the real-life scenario the $size for <code>FFI::string($array, $size)</code> is known and returned back from a different function of the C library.)</p>