print_r into a variable
Posted February 19, 2007
on:- In: PHP
- 15 Comments
print_r
is a great function. No matter how deeply nested your arrays are, print_r()
will dump all the contents out into a neatly indented textual ouput for you to look at.
But what if you need to use the output of print_r()
in a variable? Simply set the return flag like so:
$myVar = print_r($bigArray, true);
$myVar
now contains the output of print_r()
.
Genius.
15 Responses to "print_r into a variable"
Exactly what I was looking for! Thanks for posting this! Genius, as you said it!
Thanks!!
I know this post i so old now, but can you help me with this:
print_r(new MyClass()) prints the class variables and their values.
is there a straight forward way to get the class variables and their values? I want to do name=value pairs given that – very importantly – we do not know the variables in the class in the first place.
this could be due to an unknown base class, or the use of such a function
function addVar($name, $val)
{
$this->$name = $val;
}
alright, guess that’s my only way. thanks! =)
[…] (Yes, I like print_r()) […]
shukran (..means thanks :))
[…] (Yes, I like print_r()) […]
Very useful and direct to the point! Thanks!
Googled “print_r to variable”, and voila — you delivered. Thanks!
hi i used print_r($sname) in my php program can u plesae explain the meaning of this.
i write print_r($sname) in my php program.can u please explain the meaning of this.
Haha, this simple! Thank you.
1 | JP
July 21, 2008 at 3:13 pm
Very helpful and straight to the point. Gave me what I wanted in a flash. š