I need a function like var_dump($object) in php
The problem is that the Var_dump function print every thing on the screen and
I just need the answer in a variable.
I mean sth like $variable=var_dump($object); And I dont want to see anything on screen!
thanks
$variable=var_export($object,true);
you needs var_export
Maybe you use:
$variable = print_r($object, true);
U can also use the output-control functions
ob_start();
var_dump($object);
$description = ob_get_clean();
Sometimes json_encode() can be handy:
$variable = json_export($data);
Or
$variable = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
Related
Often becomes handy to debug out the dump of a variable
Laravel's
dd($v);
dump($v);
are so useful.
I wonder if there's such a function that dumps the variable as a returned string instead of printing it out.
Like in php :
var_export($var, TRUE)
Note: I'm asking for a specific Laravel function not the built in PHP function , var_dump, var_export or print_r. I already know about it.
You can use the pre tag to format your output
echo "<pre>";
print_r($data);
echo "</pre>";
Edited
If you want to store the dump in a variable, I guess you can do like below
Import use Illuminate\Support\Debug\Dumper;
$data = User::all(); // change it according to your requirement
array_map(function ($data) {
(new Dumper)->dump($data);
}, func_get_args());
I couldn't find anything, so I resort to this:
$string = var_export($data, true);
Log::info($string);
In PHP, is it possible to write out an array within an echo command (or vice-versa)?
Thank you!
Sure, print_r() has an extra option to allow it to return the formatted text, instead of directly outputting it:
echo print_r($array, true);
not sure why you'd want to but you could possibly do
$var = print_r($array, true);
echo 'stack ' . $var;
to get the result your looking for
echo print_r($myVariable, true);
The second param signals the function to return the output rather than a boolean of success.
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 :)
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();
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
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,
)
Everybody has beaten me to it: var_export()
In PHP I can say:
print_r($var);
What I can't do (at least I don't know how) is:
$var_info = print_r($var);
How can I accomplish the effect of putting the results of a print_r inside a variable?
PHP v5.3.5
When the second parameter is set to
TRUE, print_r() will return the
information rather than print it
$var_info = print_r($var,true);
$var_info = print_r($var, true);
I often have the need to view data in arrays and use the html tag <pre> to do this. However, it becomes tedious and frustrating having to type the same thing out all the time.
My question, what is your technique for avoiding this annoyance of programming with PHP?
There is no way to view it nicely formatted format except for using <pre> tag. Alternatively you can create this function and use that instead:
function pretty_print(array $array){
echo '<pre>';
print_r($array);
echo '</pre>';
}
Now instead of print_r, you can use the pretty_print. No need to type <pre> every now and then :)
Install XDebug. Besides making print_r and var_dump a lot prettier (and more useful), it also has other very handy features.
function pre_($array)
{
echo '<pre>' . print_r( $array, true ) . '</pre>';
}
You could try something like this. Note it is untested.
function html_var_dump($obj)
{
ob_start();
var_dump($obj);
$output = htmlentities(ob_get_contents());
ob_end_clean();
echo "<pre>$output</pre>";
}
You can use print_r instead of var_dump if you prefer.
I use this
function d($obj)
{
ob_start();
print_r($obj);
$output = htmlspecialchars(ob_get_clean());
echo "<pre>$output</pre>";
}
I change the default_mimetype (default text/html) php.ini setting to text/plain.
One of my favorite tricks, if printing the array is all I'm doing:
header('Content-type: text/plain');
print_r($arr);
Funny you should ask that, I just wrote a short function to save me the pain of having to do this so much.
function pre($option = "open"){
if (is_object($option) || is_array($option)):
print "<pre>";
print_r($option);
print "</pre>";
else:
$option=="open"?print "<pre>": print "</pre>";
endif;
}
If you pass an array or an object to it, it will print it inside pre tags. If you just want an opening tag, then do not pass an argument. If you want a closing tag, pass it any other argument (e.g. 1)
e.g.:
pre($result); //prints in pre tags
pre(); //just prints <pre>
print "hello";
pre(1); //just prints </pre>