Req #73811 [Opn->Wfx]: Add var_dump-like function to return output as string
| From: | yohgaki@php.net | Date: | Sun, 25 Dec 2016 23:52:29 +0000 |
| Subject: | Req #73811 [Opn->Wfx]: Add var_dump-like function to return output as string | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-206206@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=73811&edit=1
ID: 73811
Updated by: yohgaki@php.net
Reported by: pjvleeuwen at gmail dot com
Summary: Add var_dump-like function to return output as
string
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Variables related
Operating System: n/a
PHP Version: 7.1.0
Block user comment: N
Private report: N
New Comment:
Number of var_dump() args are variable, so we cannot add flag for returning string.
http://php.net/manual/en/function.var-dump.php
Use output buffer if you need to get output as string.
ob_start();
var_dump($foo);
$dump = ob_get_clean();
Previous Comments:
------------------------------------------------------------------------
[2016-12-25 21:46:14] requinix@php.net
You say "other formats"...
print_r and var_dump are debugging functions. They shouldn't be used for "practical"
purposes like serialization or storage so there isn't much room for improvement - however since
print_r can output or return a string, it makes sense for there to be output and return-a-string
versions of var_dump.
But other formats? What for? If either function doesn't provide specific information
that's would assist debugging then it should be added, but neither of them are really
appropriate for anything other than a human to read.
------------------------------------------------------------------------
[2016-12-25 21:38:18] pjvleeuwen at gmail dot com
You are right, an oversight. Thanks.
A new function (I would find the var_dump_r suggestion the most consistent) would indeed be a
possibility. But it seems that adding yet another function might not be very scaleable, these three
already feel quite ad hoc invented, while they are quite similar: represent some variable as text.
I would propose to add a 3rd $format argument (optional) to var_export (which I think has to most
concise name):
PRINTR_PARSABLE - default option, current output format of var_export
PRINTR_HUMAN - current output format of print_r
PRINTR_TYPED - current output format of var_dump
The main benefit with only these 3 constants would be allowing var_dump output format to be returned
as string in a consistent way. print_r would become redundant.
This would allow other formats to be added without a further need to be creative with similar
sounding function names.
------------------------------------------------------------------------
[2016-12-25 14:28:55] requinix@php.net
Adding $return would not work: how would var_dump() know that you aren't trying to dump a
bool(true) but passing a special argument?
This would have to be a new function, eg. var_dump_r or var_rdump.
------------------------------------------------------------------------
[2016-12-25 14:09:48] pjvleeuwen at gmail dot com
Description:
------------
Please allow var_dump to return the output as a string instead of printing. Basically adding the
same $return parameter and behavior as for var_export and print_r. Adding this parameter would be
fully backwards compatible.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=73811&edit=1