MyBB Community Forums

Full Version: Print an array to page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to work on my own mod and I'm having a lot of trouble debugging my stuff.

I'd want to print out an array to the screen:

<?php

define("IN_MYBB", 1);
define('THIS_SCRIPT', 'barn.php');

$templatelist = "index";
require_once "./global.php";

add_breadcrumb("mybreadcrumb");

$testarray = array(
    1    => "a",
    "1"  => "b",
    1.5  => "c",
    true => "d",
);
		
$barn = "<head>
<title>xyz test</title>
{$headerinclude}
</head>
<body>
{$header}
TEST " . htmlentities  (var_dump($testarray)) .  " ENDTEST 
{$footer}
</body>
</html>";

output_page($barn);

?>


But it doesn't work. The page renders but between TEST and ENDTEST there is nothing there, not even in the webpage source. How can I print my arrays to the webpage?
var_dump() returns nothing so htmlentities(var_dump()) is '' and thus you get nothing between TEST and ENDTTEST.

var_dump() does print directly though, so you should find the output in the source, at the beginning of the page, before <head> (where is your opening <html> tag by the way?).

If you're looking for a debug function that returns its data instead of echoing it, you can use var_export($data, true) or print_r($data, true) with true being the optional return parameter which makes it return the data instead of echoing it directly.

Personally I like to do this for debugging:

echo '<pre style="text-align: left">'.htmlspecialchars(print_r($whatever, true)).'</pre>';

whether you prefer print_r or var_export or even var_dump (where you'd have to use output buffer to catch the output - see the documentation) is a matter of taste; some special cases aside all of them get the job done usually.
Thanks very much!