title: UTF-8 problems in php: var_export() returns \0 null characters, and ucfirst(), strtoupper(), etc. behave strangely
body: <p>We are dealing with a strange bug in a Joyent Solaris server that never happened before (doesn't happen in localhost or two other Solaris servers with identical php configuration). Actually, I'm not sure if we have to look at php or solaris, and if it is a software or hardware problem...</p>
  
  <p>I just want to post this in case somebody can point us in the right direction.</p>
  
  <p>So, the problem seems to be in <code>var_export()</code>when dealing with strange characters.
  Executing this in the CLI, we get the expected result in our localhost machines and in two of the servers, but not in the 3rd one. All of them are configured to work with <code>utf-8</code>.</p>
  
  <pre><code>$ php -r "echo var_export('ñu', true);"
  </code></pre>
  
  <p>Gives this in older servers and localhost <strong>(expected)</strong>:</p>
  
  <pre><code>'ñu'
  </code></pre>
  
  <p>But in the server we are having problems with (<strong>PHP Version => 5.3.6</strong>), it adds <code>\0</code> null characters whenever it encounters an "uncommon" character: è, á, ç, ... you name it.</p>
  
  <pre><code>'' . "\0" . '' . "\0" . 'u'
  </code></pre>
  
  <p>Any idea on where should be looking at? Thanks in advance.</p>
  
  <hr>
  
  <p><strong>More info:</strong></p>
  
  <ul>
  <li><code>PHP version 5.3.6</code>.</li>
  <li><code>setlocale()</code> is not solving anything.</li>
  <li><code>default_charset</code> is <code>UTF-8</code> in <code>php.ini</code>.</li>
  <li><code>mbstring.internal_encoding</code> is set to <code>UTF-8</code> in <code>php.ini</code>.</li>
  <li><code>mbstring.func_overload = 0</code>.</li>
  <li>this happens in both CLI (example) and web application (php-fpm + nginx).</li>
  <li><code>iconv</code> encoding is also <code>UTF-8</code></li>
  <li>all files <code>utf-8</code> encoded.</li>
  </ul>
  
  <p><code>system('locale')</code> returns:</p>
  
  <pre><code>LANG=en_US.UTF-8
  LC_CTYPE="en_US.UTF-8"
  LC_NUMERIC="en_US.UTF-8"
  LC_TIME="en_US.UTF-8"
  LC_COLLATE="en_US.UTF-8"
  LC_MONETARY="en_US.UTF-8"
  LC_MESSAGES="en_US.UTF-8"
  LC_ALL=
  </code></pre>
  
  <hr>
  
  <p><strong>Some of the tests done so far (CLI):</strong></p>
  
  <p>Normal behaviour: </p>
  
  <pre><code>$ php -r "echo bin2hex('ñu');" =&gt; 'c3b175'
  $ php -r "echo mb_strtoupper('ñu');" =&gt; 'ÑU'
  $ php -r "echo serialize(\"\\xC3\\xB1\");" =&gt; 's:2:"ñ";'
  $ php -r "echo bin2hex(addcslashes(b\"\\xC3\\xB1\", \"'\\\\\"));" =&gt; 'c3b1'
  $ php -r "echo ucfirst('iñu');" =&gt; 'Iñu'
  </code></pre>
  
  <p>Not normal: </p>
  
  <pre><code>$ php -r "echo strtoupper('ñu');" =&gt; 'U' 
  $ php -r "echo ucfirst('ñu');" =&gt; '?u' 
  $ php -r "echo ucfirst(b\"\\xC3\\xB1u\");" =&gt; '?u' 
  $ php -r "echo bin2hex(ucfirst('ñu'));" =&gt; '00b175'
  $ php -r "echo bin2hex(var_export('ñ', 1));" =&gt; '2727202e20225c3022202e202727202e20225c3022202e202727'
  $ php -r "echo bin2hex(var_export(b\"\\xC3\\xB1\", 1));" =&gt; '2727202e20225c3022202e202727202e20225c3022202e202727'
  </code></pre>
  
  <p>So the problem seems to be in <code>var_export()</code> and <a href="http://www.php.net/manual/en/language.types.string.php#language.types.string.details" rel="nofollow">"string functions that use the current locale but operate byte-by-byte" <sup><em>Docs</em></sup></a> (view @hakre's answer).</p>