php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63745 Var_dump on null characters
Submitted: 2012-12-11 19:04 UTC Modified: 2012-12-12 01:40 UTC
From: jesper dot hag at gmail dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.3Git-2012-12-11 (Git) OS: CentOS
Private report: No CVE-ID: None
 [2012-12-11 19:04 UTC] jesper dot hag at gmail dot com
Description:
------------
*Note* My PHP version is actually 5.3.3
The problem I've come up to is that printing a string with var_dump (for debugging 
purpose eg.) that contains null-characters are counted in the string length but 
are not in any way displayed in the var_dump output.

I'm not sure this should be considerate a bug or a feature request because it is 
something I want and perhaps not something that per design should be in this way.

Test script:
---------------
<?php
$string = "123456"; // len 6
$string[0] = ''; (or = null, or = "\0")

var_dump($string); // Outputs string(6) "23456"

?>

Expected result:
----------------
I would expect the above script to output something like string(6) "\0123456", 
that - in my world - would make more sense. 


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-12 01:40 UTC] aharvey@php.net
-Status: Open +Status: Not a bug -Package: *General Issues +Package: Variables related
 [2012-12-12 01:40 UTC] aharvey@php.net
var_dump() dumps the contents of the string as is, so the null byte is literally a null byte in the output. If you run your test script through hexdump, you get this:

adamh@swiftdesk7:~$ php /tmp/test.php | hexdump -C
00000000  73 74 72 69 6e 67 28 36  29 20 22 00 32 33 34 35  |string(6) ".2345|
00000010  36 22 0a                                          |6".|
00000013

You can see the null byte in the output.

What you probably want instead of var_export(), which will escape non-printable characters: for instance, your test string is exported as:

'' . "\0" . '23456'
 [2012-12-12 05:47 UTC] jesper dot hag at gmail dot com
Thanks for your input! Never thought of var_export() but of course that does 
exactly what I was looking for.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Oct 07 18:01:28 2024 UTC