putting print_r results in variable - php

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

Related

Laravel: does exists a equivalent of var_export($var, True) helper?

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

PHP - Is concatenation possible between echo and print_r

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.

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

What does "1" mean at the end of a php print_r statement?

My print_r($view) function yields:
View Object
(
[viewArray:View:private] => Array
(
[title] => Projet JDelage
)
)
1 <--------------
What does the "1" at the end mean? The PHP manual isn't very clear on how to parse the output of print_r.
You probably have echo print_r($view). Remove the echo construct. And... what need do you have to parse its output? There are certainly much better ways to solve your problem.
print_r called with one argument (or with its second argument set to false), will echo the representation of its parameter to stdout. If it does this, it returns TRUE. Thus if you echo print_r($foo) you will print the contents of foo, followed by a string representation of the return value (which is 1).
When using print_r to return, than than output/print the value, pass the 2nd parameter which defines return as true.
echo print_r($view, true);
This is useful if you want to save the results to a variable or concatenate with another string.
$var = 'The array is: ' . print_r($view, true);

How to store all in a variable instead of echoing everything separately?

please see: http://pastebin.com/5za3uCi1
I'm quite new to php and I'm editing the ventrilo status script. What I'd like it to do is that it stores everything in one big variable for easy parsing instead of using separate echo's. Can someone tell me how I can accomplish this?
Thanks,
Dennis
You can use the output buffer and get the contents of it:
ob_start();
echo 'foobar';
$contents = ob_get_contents(); // now contains 'foobar'
ob_end_clean();
declare a variable at the beginning, say $data or whatever. then, replace the echo calls:
echo "hello";
with this:
$data .= "hello";
then return the $data variable at the end of the function.
Instead of the echo, you can use a simple affectation :
$request = "CVentriloStatus->Request() failed. <strong>$stat->m_error</strong><br><br>\n";
But you'll soon have issues to manage multiple variables.
You could create an object to handle and store your information, but If you need something easy to set up and simple to operable, I'd go for arrays :
$ventriloStatus = array();
$ventriloStatus['requestObj'] = $stat->Request();
$ventriloStatus['requestMsg'] = "CVentriloStatus->Request() failed. <strong>$stat->m_error</strong><br><br>\n";
Add your data using keys.
Then retrieve the value easily :
echo $ventriloStatus['requestMsg'];
You can even parse your data using a simple loop
foreach($ventriloStatus as $key => $value){
echo $key.' : '.$value.'<br />';

Categories