6

since I am using json, and ajax it's annoting I cn't pass the value on a valid json.

is there away to just return the value of var dump without it echo,being output to the browser.

e.g.

$data = 'my_data';
get_var_dump($data);//not real func
//should do nothing.
$get['data'] = get_var_dump($data);
$get['error']= false;
echo json_encode($get);
//should be something like 
//{"data,"string(7) my_data","error":false}

or the print_r equivalent I just want to to be assigned to a var instead of outputting it.

or if ur a wordpress fan, difference between bloginfo('url'); and get_bloginfo('url'); should make it easier :)

Val
  • 17,336
  • 23
  • 95
  • 144
  • Are you saying you want to print the values to the screen with out using the echo keyword? – k to the z Apr 14 '11 at 15:47
  • ok the `var_dump` should be `function var_dump($str){ return $str; }` not `function var_dump($str){ echo $str;}` so that way I can assign it to a varible – Val Apr 14 '11 at 15:49

4 Answers4

16

Sure you can! To do that you will need two PHP buffer functions: ob_start and ob_get_clean. The first one starts buffering, when the second is getting value and cleaning buffer. Example:

ob_start();
var_dump($array);
$value = ob_get_clean();
Robik
  • 6,047
  • 4
  • 31
  • 41
  • 3
    Dont understand the downvotes. Because `var_export()`, `var_dump()` and `print_r()` have all slightly different formats, this one should at least get mentioned. – KingCrunch Apr 14 '11 at 15:54
  • 1
    What's wrong with my answer? It is working.. I don't understand why ppl are downvoting .. – Robik Apr 14 '11 at 15:55
  • 2
    Downvoters: Please note that this is *exactly* how [print_r](http://us3.php.net/manual/en/function.print-r.php) and [var_export](http://us3.php.net/manual/en/function.var-export.php) work internally to return instead of outputting. So if you want the different format of `var_dump` (including types and sizes) this is actually the proper method... – ircmaxell Apr 14 '11 at 16:03
12

print_r has the option for a second parameter. When set to true, it returns the dump as an array instead of displaying it.

http://us2.php.net/print_r

Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
  • 2
    thats what I was looking for... so used to `print_r` the normal way I didnt even know it exists but thnx :) – Val Apr 14 '11 at 15:51
  • 1
    Pedantic note: `print_r` is a very different output to `var_dump`. Print_r only shows the structures and approximate values (printable value). `var_dump` actually shows the exact values and structure including the string length. I personally find no use for `print_r` since `var_dump` has so much extra valuable information... – ircmaxell Apr 14 '11 at 16:06
6

Check the var_export() function:
http://php.net/manual/en/function.var-export.php

You pass a variable to it and a second boolean parameter, if the second parameter is true the functions return a string with a rapresentation of the variable:

<?php
$a = array(1, 2);
$dump = var_export($a, true);

print $dump;
?>

$dump contains something like

array (
  0 => 1,
  1 => 2,
)
Davide Gualano
  • 12,813
  • 9
  • 44
  • 65
3

Everybody has beaten me to it: var_export()

Adam Pointer
  • 1,492
  • 8
  • 17