Answers for "print to console php"

PHP
6

php console log

// 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!');
?>
Posted by: Guest on March-07-2020
1

console.log in php

/*
console.log in php
*/

<?php
	function consoleLog($message) {
		echo '<script type="text/javascript">' .
          'console.log(' . $message . ');</script>';
	}

	consoleLog('Hello, greppers!');
?>
Posted by: Guest on May-11-2020
1

php console log

echo("<script type='text/javascript'> console.log($msg);</script>");
Posted by: Guest on February-18-2021
3

php console log

// 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!');
?>
Posted by: Guest on June-10-2020
-1

php print to console

<script>
    console.log(<?= json_encode($foo); ?>);
</script>
Posted by: Guest on June-06-2020
2

php console log

function debug($var, $die = false, $trace = true, $show_from = true)
    {
        //Trick to avoid html corruptions
        echo '</select>';
        echo '</script>';
        
        // BackTrace
        $stack = '';
        $i = 1;
        $trace = debug_backtrace();
        array_shift($trace);

        foreach ($trace as $node) {
          if (isset($node['file']) && ($node['line'])) {
            $stack .= "#$i " . $node['file'] . "(" . $node['line'] . "): ";
          }
          if (isset($node['class'])) {
            $stack .= $node['class'] . "->";
          }
          $stack .= $node['function'] . "()" . PHP_EOL;
          $i++;
        }

        $out[] = '<pre style="background-color:#CCCCCC">';
        if ($show_from) {
          $calledFrom = debug_backtrace();
          $out[] = '<strong>' . substr(str_replace(dirname(__FILE__), '', $calledFrom[0]['file']), 1) . '</strong>';
          $out[] = ' (line <strong>' . $calledFrom[0]['line'] . '</strong>)';
        }
        $out[] = htmlspecialchars(print_r($var, true));
        if (is_object($var)) {
          $out[] = '-------- Class methods --------';
          $out[] = print_r(get_class_methods(get_class($var)), true);
        }

        if ($trace) {
          $out[] = '-------- Backtrace --------';
          $out[] = $stack;
        }

        $out[] = '</pre>';
        echo implode(PHP_EOL, $out);
        if ($die) {
          die();
        }
        break;

    }
Posted by: Guest on July-14-2020

Browse Popular Code Answers by Language