How to print Console.log in PHP
What is console.log( )
MDN web docs explain console.log()
very well.
The
console.log()
method outputs a message to the web console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.MDN WEB DOCS
If you are a Frontend Developer or who use web browser, you need to know well about this method. As a junior web dev, I also love printing with this method but sometime I meet obstacle.
Work with PHP
My company has a website that made by Wordpress. So i need to fix bug in in PHP environment the first thing i need to find is this.
“How can i debug with console.log()
”
For this reason, I decided to wrap up that method today.
How can i print Console.log( ) in PHP?
The principle is simple. PHP is a backend environment, it draws code into pieces.
<script>
console.log('this is a Variable:', $variable);
</script>
If we want to write console.log( ) like above then,
echo "<script>console.log('this is a Variable: " . $variable. "' );</script>";
So easy right? If it is little hard to understand than you can break down it.
echo "<script>"
echo "console.log('this is a Variable: " . $variable. "' );"
echo "</script>"
This way, you will be able to understand the principle of PHP to output pieces one by one.