How to echo (or print) to the js console with php
This will work with either an array, an object or a variable and also escapes the special characters that may break your JS :
function debugToConsole($msg) {
echo "<script>console.log(".json_encode($msg).")</script>";
}
Edit : Added json_encode
to the echo
statement. This will prevent your script from breaking if there are quotes in your $msg
variable.
<?php
echo '<script>console.log("Your stuff here")</script>';
?>