php-src icon indicating copy to clipboard operation
php-src copied to clipboard

var_dump() function displays certain strings incorrectly

Open canajun2eh opened this issue 4 years ago • 8 comments

Description

When the var_dump() function is asked to display the contents of a string that contains possible HTML (the text is enclosed in angle brackets) , no attempt is made to show those angle brackets . This results in the browser's rendering engine trying to execute the HTML. If the string contains HTML entities, no attempt is made to show the actual HTML entity; the browser's rendering engine will then try to display the graphic equivalent of that HTML entity.

For example, if the following is executed:

$string = "some text <with other text inside angle brackets>";
var_dump($string);

we are told that $string is a string with length 49, having the value some text . Note the trailing blank. The missing text is interpreted by the browser's rendering engine as illegal HTML and is therefore discarded. Similarly, if the text inside the angle brackets is legal HTML, the browser's rendering wngine will attempt to execute that HTML. This behaviour is undesirable.

The var_dump function should ensure that, when the input is a string, the value of that string is fully printable by replacing the angle brackets with their corresponding HTML entities &lt; and &gt;.

In addition to this, any ampersands in the string should be replaced by their HTML entity &amp; so that the value displayed by the var_dump function reflects the actual contents of the string.

PHP Version

not relevant

Operating System

not relevant

canajun2eh avatar Jan 28 '22 21:01 canajun2eh

What about the times when PHP is running and the output will not be going to a browser?

damianwadley avatar Jan 28 '22 21:01 damianwadley

Good question. Under what circumstances would you use the var_dump function where its output isn't sent to a browser?

I can only think of one valid use of this function: "debug".

canajun2eh avatar Jan 28 '22 21:01 canajun2eh

@canajun2eh var_dump is used in CLI for debugging purposes as well. We could of course just escape special characters when you're in the browser. Since that's PHPs primary target it is somewhat surprising the output isn't escaped.

That said, maybe consider using something more sophisticated like Symfony's VarDumper component. It'll escape the output for you and has coloring, collapsing, works better on deeply nested data structures, etc.

iluuu1994 avatar Jan 28 '22 22:01 iluuu1994

var_dump() outputs the bytes of string, and it's up to the receiver to interpret that. If you send an UTF-8 encoded ü in a document that's interpreted as ISO-8859-1 encoded, you don't get that magically fixed either. Okay, there are output_encoding and friends, so it might make sense to introduce a respective INI directive, but the current behavior is certainly not a bug. I'm re-classifying as feature request requiring an RFC. An alternative would be to re-classify as doc issue.

cmb69 avatar Jan 29 '22 23:01 cmb69

I disagree.

You are incorrect in changing the title and the class of this issue. Please restore the original title and make this a "bug" again. Your discussion about the string value possibly containing something that is eventually rendered as ü is not relevant.

The var_dump() function is clearly intended to support debugging ONLY; it should not be used in a working program. The PHP manual is quite clear on this: "This function displays structured information ..." and "As with anything that outputs its result directly to the browser, the output-control functions can be used to capture the output of this function, and save it in a string (for example)."

The PHP language has no built-in tool that permits the programmer to determine the raw value of a string. Neither print nor echo do this; they send the raw string value to the browser which then takes action as determined by the text it has received. The var_dump() function does exactly the same thing: it sends the raw string value to the browser.

I realize that the output control functions can be used to capture the result of the var_dump() function to a string, which can then undergo a transformation using str_replace(array('&', '<', '>'), array('&amp;', '&gt;', '&lt;'), $capturedText); The transformed captured text can then be sent to the browser. This process is cumbersome and requires a great deal of before-the-fact planning. There is also no guarantee that captured text that is not part of a string value will not be transformed. The output control functions are therefore not suited to ad-hoc debugging.

I also realize that the browser's "View page source" (or whatever your favourite browser calls this) function can be used to determine the actual string value. Again, this process is cumbersome and is not well-suited to ad-hoc debugging.

I submit that the var_dump() function is incorrect in sending raw string values to the browser; it should be performing a transformation of these string values using an equivalent of the str_replace() function mentioned above. Of course, the length of the string values should be calculated before the transformation takes place.

canajun2eh avatar Jan 30 '22 12:01 canajun2eh

The var_dump() function is clearly intended to support debugging ONLY; it should not be used in a working program.

In the debugging enviroment xdebug extension is often active, so the improved var_dump can be used with ease https://xdebug.org/docs/develop#display

The transformed captured text can then be sent to the browser. This process is cumbersome and requires a great deal of before-the-fact planning.

You can write a simple function (e.g. var_dump_html) or use xdebug or VarDumper - it's not that big deal.

Again, this process is cumbersome and is not well-suited to ad-hoc debugging.

var_dump is a very crude way to debug the code, the xdebug integration with the IDE is intended for that, but I understand that var_dump doesn't require any setup and can be tempting.

I submit that the var_dump() function is incorrect in sending raw string values to the browser

For console uses this change would result in the same problem, but in reverse, which would be cumbersome and requires a great deal of fixing in the existing codebase.

Of course var_dump could check if php is runned from browser or console, but I bet there is quite a bit of phpunit tests which will fail after such change or environments incorrectly classified as a browser. With other easy to use alternatives, there is little to no point in breaking current behavior.

Is this a bug? No. There is nothing that would suggest that var_dump should return html in the documentation. It might be worth to add such notice, but not mentioning a feature should not suggest it's presence.

Though It might be good idea to add a new function with html encoding.

KapitanOczywisty avatar Jan 30 '22 14:01 KapitanOczywisty

IMO not a bug.

cmb69 avatar Jan 30 '22 17:01 cmb69

I agree, this is clearly not a bug. As OP insisted on a bug classification, I'm closing this issue.

nikic avatar Jan 30 '22 17:01 nikic