PDA

View Full Version : print_r replacement


Boofo
04-25-2011, 08:20 PM
I just found this little snippet of code that works great for replacing print_r when working with code, and thought I would share it here. It prints out like this:

Array
(
[0] => 5
[1] => 6
[2] => 7
)


Here is the code:

if (!function_exists("preprint")) {
function preprint($s, $return=false) {
$x = "<pre>";
$x .= print_r($s, 1);
$x .= "</pre>";
if ($return) return $x;
else print $x;
}
}


@usage:

preprint($x); <- where $x = array()


NOTE: The optional 2nd parameter can be set to "true", so the html-code is returned.

I haven't quite figured out what the 2nd perimeter actually does as no code has shown up for me when that has been enabled.

Disasterpiece
04-26-2011, 08:24 AM
var_dump() / var_export() ?

Why re-invent the wheel?

Boofo
04-26-2011, 08:25 PM
That does not format it so it is easily readable.

If you don't like it, don't use it. Keep the sarcastic comments to yourself.

Disasterpiece
04-26-2011, 08:31 PM
Dude, what is wrong with you!?

Just trying to help, sorry :/

Boofo
04-26-2011, 08:53 PM
I just woke up and that post was the first thing I saw. Maybe I read it wrong, If so, I apologize.

But, var_dump or var_export shows it all in one line. The pre tags format it so it is easier to view. I had been using var_dump until I found this.