• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2240
  • Last Modified:

var_dump to variable

How can I var_dump to a variable?

This does NOT work
<?php
 
$zzz=array('111','222',333);
$qqq = var_dump($zzz);
 
?>

Open in new window

0
hankknight
Asked:
hankknight
  • 5
  • 2
  • 2
1 Solution
 
Beverley PortlockCommented:
<?php
 
$zzz=array('111','222',333);
$qqq = print_r($zzz);

echo $qqq;
?>
0
 
ncooCommented:
Try:
<?php
 
$zzz=array('111','222',333);
$qqq = print_r($zzz,true);
 
echo $qqq;
?>

Open in new window

0
 
ncooCommented:
With print_r() you need to set the return parameter to true.

If you would like to capture the output of print_r(), use the return  parameter. If this parameter is set to TRUE, print_r() will return its output, instead of printing it (which it does by default).

http://uk2.php.net/print_r
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Beverley PortlockCommented:
LOL! Thanks ncoo - I thought I had put that in, obviously not.....
0
 
hankknightAuthor Commented:
Thank you all but this does not answer my question.  I am aware of print_r($zzz,true);

But print_r does NOT return all the same information as var_dump.

Is there anything like var_dump($zzz,true);
 
?
0
 
ncooCommented:
Try this:
ob_start();
 
$zzz=array('111','222',333);
var_dump($zzz);
 
$qqq = ob_get_clean();
echo $qqq;

Open in new window

0
 
ncooCommented:
What we're doing is using PHP output controls to catch the var_dump and setting the captured output to your qqq value.
0
 
hankknightAuthor Commented:
ncoo, your idea is a hack and not what I am looking for.

Is the consensus that no PHP function is capable of doing what I am asking (without output buffering)?
0
 
ncooCommented:
You could try: http://pear.php.net/package/Var_Dump

http://pear.php.net/package/Var_Dump/docs/latest/li_Var_Dump.html

I have never used it myself so can't comment on which method is faster.

It may also be worth looking at: http://uk2.php.net/manual/en/function.var-export.php

var_export() gets structured information about the given variable. It is similar to var_dump()  with one exception: the returned representation is valid PHP code.

$qqq = var_export($zzz,true);

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now