Why does var_dump not show null bytes?

I had 2 cases when zero bytes \0 are added to my data.

1. Place an object in an array

 class myClass { private $var; function __construct() {} } $myObject = (array) new myClass(); var_dump(array_map("addslashes", array_keys($myObject))); 

Outputs:

 array(1) { [0]=> string(14) "\0myClass\0var" } 

2. When decrypting encrypted data:

 function encrypt_data($data) { return base64_encode(mcrypt_encrypt(MCRYPT_BLOWFISH , SALT , $data , MCRYPT_MODE_ECB)); } function decrypt_data($data) { $data = base64_decode($data); return mcrypt_decrypt(MCRYPT_BLOWFISH , SALT , $data , MCRYPT_MODE_ECB); } $data = '12345678901234567 aasdasd'; $edata = encrypt_data($data); var_dump(addslashes(decrypt_data($edata))); 

Outputs:

 string(39) "12345678901234567 aasdasd\0\0\0\0\0\0\0" 

But I would never notice \0 , if not addslashes . Why doesn't var_dump() just show them? var_dump("Hello\0 World"); for example, displays "Hello World". In my opinion, a poor presentation of the data. And as far as I know, \0 byte is the end of the char array (string in PHP) in C and PHP is implemented in C.

+5
source share
1 answer

var_dump displays the lines as is. If your string contains a NUL byte, that NUL byte will be output as is. The problem is that the NUL byte is usually not displayed like anything in the browser, or perhaps even on the command line.

Indicates that the displayed string length is different from the displayed. A string(42) "abc" probably contains a lot of "hidden" characters. Obviously, it gets harder and harder to look in the eyes, the longer your string ...

var_export makes NUL bytes and the like much more obvious.

+2
source

Source: https://habr.com/ru/post/1245323/


All Articles