Skip to content

Instantly share code, notes, and snippets.

@Avi-E-Koenig
Last active September 9, 2022 15:00
Show Gist options
  • Save Avi-E-Koenig/d0317bb35fcf4bcd0b32696d5d8675fc to your computer and use it in GitHub Desktop.
Save Avi-E-Koenig/d0317bb35fcf4bcd0b32696d5d8675fc to your computer and use it in GitHub Desktop.
Basic output to console of object
<?php
function echoPrettyJson($variable,$key)
{
//pseudo
if($production) return;
try {
$key_name = $key ? $key : 'output';
$content = new stdClass();
$content->{$key_name} = $variable;
echo '<pre>' . json_encode($content, JSON_PRETTY_PRINT) . '</pre>';
}
//catch exception
catch (Exception $e) {
echo 'Message: ' . $e->getMessage();
}
}
function echoAsJSConsole($variable,$key)
{
//pseudo
if($production) return;
try {
$key_name = $key ? $key : 'output';
$content = new stdClass();
$content->{$key_name} = $variable;
echo '<script>console.log(' . json_encode($content, JSON_PRETTY_PRINT) . ');</script>';
}
//catch exception
catch (Exception $e) {
echo 'Message: ' . $e->getMessage();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment