Skip to content

var_dump() $return like print_r to allow using it in ob_start() callbacks without fatal #18340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kkmuffme opened this issue Apr 16, 2025 · 3 comments

Comments

@kkmuffme
Copy link

Description

function my_cb($b) {
    ob_start();
    var_dump($b);
    $r = ob_get_clean();
    return $b . $r;
}

ob_start('my_cb');
echo "hello";

Will result in:

Fatal error: ob_start(): Cannot use output buffering in output buffering display handlers

When using:

function my_cb($b) {
    $r = print_r($b, true);
    return $b . $r;
}

it works without error (now, this wasn't the case in earlier PHP versions where it had the same issue as can be seen
in the old docs php/doc-en@e8f4c44#diff-a18323268abbc44bce515215167dfae5d0fb378c63ea36bea0f6e537a6a0114aL115)

What's the reason why print_r with return (which uses buffering internally) can be called in an ob callback now, but ob_start still can't?
Anyway though - given this, var_dump should have a 2nd argument $return to allow returning, so it can be used in ob_start callbacks, where ob_start cannot be used to get it's value

@MorganLOCode
Copy link

One of the problems with giving var_dump a second argument is that it already takes an arbitrary number of arguments.

@kkmuffme
Copy link
Author

Oh right, how about a new var_return or var_dump_return then?

@Piskvor
Copy link

Piskvor commented Apr 25, 2025

@kkmuffme : That's literally var_export($whatever, true);.

https://www.php.net/manual/en/function.var-export.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants