Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDylan Hobbs
9,279 PointsWhere does print_r output?
here is my code:
<?php
function generate_options_css($newdata) {
$data = $newdata;
$uploads = wp_upload_dir();
$css_dir = get_template_directory() . '/assets/css/dynamic-css/'; // Shorten code, save 1 call
/** Save on different directory if on multisite **/
if(is_multisite()) {
$gt_uploads_dir = trailingslashit($uploads['basedir']);
} else {
$gt_uploads_dir = $css_dir;
}
ob_start(); // Capture all output (output buffering)
echo '<pre>';
print_r($data);
echo '</pre>';
require( $css_dir . 'style.php' ); // Generate CSS
$css = ob_get_clean(); // Get generated CSS (output buffering)
file_put_contents( $gt_uploads_dir . 'options.css', $css, LOCK_EX); //100% safe - ignore theme check nag
}
?>
I am trying to output the object $data but can't seem to figure out where I can view it?
Where does echo or print_r output the data to? I am used to javascript with console.log. Where can I view the object?
2 Answers
Nazmus Sarwar
5,625 PointsPHP works on the browser. So if you are using Workspaces, you can just hit run (top right corner) and you should see the output in your browser.
Lin Lu
29,171 PointsYou might have noticed that a few lines above print_r
, there's a function called ob_start()
. According to the PHP documentation, what this function does is that it turns on the output buffering, so no output will be sent to the browser, instead, the output will be stored in an internal buffer. To output what is stored in the internal buffer, use ob_end_flush()
. Alternatively, ob_end_clean()
will silently discard the buffer contents.
In your code, $css = ob_get_clean();
stored the content in the buffer to the variable $css
, which is later on written to a file, so if you want to view the content, you may open the file, whose path is $gt_uploads_dir . 'options.css'
.
If you want to use console.log()
to output to the console, I think you can echo out script tags with echo
in PHP and insert your JavaScript code between the script tags. For example:
<?php
echo "<script>console.log('Message...');</script>";
?>
Dylan Hobbs
9,279 PointsDylan Hobbs
9,279 PointsNot working on workspaces. This is local. The bug is in a wordpress theme, I am trying to find out where it is coming from. So I am trying to print out the $data object before the css is called in. I don't see anything in browser after I enter the code in though? Should it still show up if it is within a wordpress theme?
Dylan Hobbs
9,279 PointsDylan Hobbs
9,279 PointsI guess what im asking is, if the file i am trying to find the bug is in a functions.php type file, then how would I output the object using print_r?
I know I can use print_r on the index.php to output the object, but I want to output the object at that point, just as I would with console.log