Val
Val

Reputation: 17542

php var_dump equivelant or print_r

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 :)

Upvotes: 6

Views: 6763

Answers (4)

Adam Pointer
Adam Pointer

Reputation: 1492

Everybody has beaten me to it: var_export()

Upvotes: 3

Davide Gualano
Davide Gualano

Reputation: 12993

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,
)

Upvotes: 6

Mr. Llama
Mr. Llama

Reputation: 20919

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

https://www.php.net/print_r

Upvotes: 13

Robik
Robik

Reputation: 6127

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();

Upvotes: 16

Related Questions