Skip to content

Instantly share code, notes, and snippets.

@zoerab
Created March 7, 2016 20:14
Show Gist options
  • Select an option

  • Save zoerab/3eb4bd433747215d57eb to your computer and use it in GitHub Desktop.

Select an option

Save zoerab/3eb4bd433747215d57eb to your computer and use it in GitHub Desktop.
function var2console($var, $name='', $now=false)
{
if ($var === null) $type = 'NULL';
else if (is_bool ($var)) $type = 'BOOL';
else if (is_string ($var)) $type = 'STRING['.strlen($var).']';
else if (is_int ($var)) $type = 'INT';
else if (is_float ($var)) $type = 'FLOAT';
else if (is_array ($var)) $type = 'ARRAY['.count($var).']';
else if (is_object ($var)) $type = 'OBJECT';
else if (is_resource($var)) $type = 'RESOURCE';
else $type = '???';
if (strlen($name)) {
str2console("$type $name = ".var_export($var, true).';', $now);
} else {
str2console("$type = " .var_export($var, true).';', $now);
}
}
function str2console($str, $now=false)
{
if ($now) {
echo "<script type='text/javascript'>\n";
echo "//<![CDATA[\n";
echo "console.log(", json_encode($str), ");\n";
echo "//]]>\n";
echo "</script>";
} else {
register_shutdown_function('str2console', $str, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment