Prettier, customizable print_r function

When developing random stuff with PHP I tend to rely heavily on various variable printing so I made a small function that I use to print variables the way I want to.

<?php
	function print_pre($text, $ret = false, $inStyles = array()) {
	$aStyles = array("-webkit-border-radius" => "3px", 
		"-moz-border-radius" => "3px", 
		"border-radius" => "3px",
		"background-color" => "#F0F0F0", 
		"padding" => "3px",
		"border" => "1px solid #A8A8A8");
	
	if (is_array($inStyles))
	{
		foreach($inStyles as $key => $value)
			$aStyles[$key] = $value;
	}
		
	$applyStyles = array();	
	foreach($aStyles as $key => $value)
		array_push($applyStyles, $key . ": " . $value . ";");

	$return = "<pre style='".implode(" ", $applyStyles)."'>" . print_r($text, true) . "</pre>";
	if($ret) return $return;
	print($return);
}
?>

It’s pretty straightforward – it takes 3 arguments, 1st being variable we want to display, 2nd if we want the return string returned or directly printed on page and finally CSS styling pairs as associative array.

Few examples of use

Default print

print_pre("Test string", true);

Result:

Test string

Custom style

print_pre("Test string", false, array("background-color" => "#000", "color" => "#fff"));

Result:

Test string

Printing an object

$array = array("name" => "david", "age" => (date("Y") - 1987), "gender" => "male");
$object = json_decode(json_encode($array), FALSE);
print_pre($object, false, array("background-color" => "blue", "color" => "#fff"));

Result:

stdClass Object
(
    [name] => david
    [age] => 30
    [gender] => male
)

Getting the return print string of regular array and echoing it as a variable

$array = array("name" => "david", "age" => (date("Y") - 1987), "gender" => "male");
$value = print_pre($array, true, array("background-color" => "yellow", "color" => "gray"));
echo $value;

Result:

Array
(
    [name] => david
    [age] => 30
    [gender] => male
)