// Assuming you are wishing to log to the JS Console...
<?php
function consoleLog($msg) {
echo '<script type="text/javascript">' .
'console.log(' . $msg . ');</script>';
}
consoleLog('Hello, console!');
?>
echo("<script type='text/javascript'> console.log($msg);</script>");
/*
console.log in php
*/
<?php
function consoleLog($message) {
echo '<script type="text/javascript">' .
'console.log(' . $message . ');</script>';
}
consoleLog('Hello, greppers!');
?>
<?php
function console_log($object){
echo "<script>console.log(".json_encode(var_export($object, true)).");</script>";
}
console_log('wow!');
?>
// A little correction / improvement to @Kaotik's answer:
<?php
function consoleLog($msg)
{
echo '<script type="text/javascript">console.log('
. str_replace('<', 'x3C', json_encode($msg))
. ');</script>';
}
consoleLog('Hello, console!');
?>
<?php
function console_log($output, $with_script_tags = true)
{
$js_code = 'console.log(' . json_encode($output, JSON_HEX_TAG) . ');';
if ($with_script_tags)
{
$js_code = '<script>' . $js_code . '</script>';
}
echo $js_code;
}
#Let's use this :)
$name = "Omar Faruk"; // string type variable
console_log($name); // "Omar Faruk"
console_log(gettype($name)); // string
// now go to the browser and check the console
<?php
function console_log($output, $with_script_tags = true) {
$js_code = 'console.log(' . json_encode($output, JSON_HEX_TAG) .
');';
if ($with_script_tags) {
$js_code = '<script>' . $js_code . '</script>';
}
echo $js_code;
}
<?php
function consoleLog($msg) {
echo '<script type="text/javascript">console.log('. $msg .');</script>';
}
consoleLog("'Hello world!'");
?>