print_r to variable?
Moderator: General Moderators
print_r to variable?
I use print_r($_SESSION) to print out all the nested arrays in the session. is there a way to create a variable that is a string of what would be printed using print_r?
I basically want to do this:
$variable = print_r($_SESSION);
except obviously that won't work.
I basically want to do this:
$variable = print_r($_SESSION);
except obviously that won't work.
Code: Select all
function capture_print_r($output){
ob_start();
print_r($output);
return ob_get_clean();
}- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
3 minutes in the manual:
bool print_r ( mixed expression [, bool return] )
bool print_r ( mixed expression [, bool return] )
Code: Select all
$output = print_r($var, true);FINALLY!! I thought I must have been crazy...
viewtopic.php?t=37033
I coulnt'd remember where I saw the ability to return output rather than output it. I didn't realize it wasn't a function, but an argument of the function I was using... silly me.
viewtopic.php?t=37033
I coulnt'd remember where I saw the ability to return output rather than output it. I didn't realize it wasn't a function, but an argument of the function I was using... silly me.
- dibyendrah
- Forum Contributor
- Posts: 491
- Joined: Wed Oct 19, 2005 5:14 am
- Location: Nepal
- Contact:
Code: Select all
<?php
session_start();
$HTTP_SESSION_VARS['X'] = 'ABC';
$HTTP_SESSION_VARS['Y'] = 'XYZ';
$output = print_r($_SESSION, true);
//print_r($output);
if(is_array($output)){
print_r($output);
}else{
var_dump($output);
}
?>in the above code I tried to return the array output to the $output varaible instead of outputting the variable. When I do print_r($output) it does the output, but when I check the variable if it is array or not by is_array() it don't check it as array but var_dump shows it as array. What's wrong with this ?
Maybe I am wrong ..
Dibyendra
Last edited by dibyendrah on Thu Aug 17, 2006 12:59 am, edited 1 time in total.
- dibyendrah
- Forum Contributor
- Posts: 491
- Joined: Wed Oct 19, 2005 5:14 am
- Location: Nepal
- Contact:
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Note the following from the command line
Code: Select all
[feyd@home]>php -r "$foo['XY'] = 'I am foo.'; $foo['AB'] = 'I am foo, too?'; var_dump(gettype($foo),gettype(print_r($foo, true)));"
string(5) "array"
string(6) "string"- dibyendrah
- Forum Contributor
- Posts: 491
- Joined: Wed Oct 19, 2005 5:14 am
- Location: Nepal
- Contact: