How to print JavaScript console with PHP ?
The echo command is used in PHP to print any value to the HTML document. Use <script> tag inside echo command to print to the console.
Syntax:
echo "This is GeeksforGeeks Tutorial."; echo variable_name;
Note: The echo command will print the HTML document. This feature can be used to put JavaScript statements in the HTML page.
Example 1: This example illustrates how to use echo keyword to display content.
<?php // Declare variable and store the string $var1 = "GeeksforGeeks"; $var2 = "A computer science portal"; // Use echo keyword to display result echo $var1 . "\n" . $var2 ; ?> |
Output:
GeeksforGeeks A computer science portal
Dot ‘.’ operator is used to joining two strings in PHP. You can print multiple variables by joining with a dot ‘.’ operator. The echo keyword is used to display the values of variables var1 and var2 to the HTML document which is readable by the browser normally. This property can be taken into an advantage in writing to JavaScript console using PHP. The JavaScript code can be written in the echo section and taken to the HTML document.
Example 2: This example illustrates how to print a string into the console.
<?php // Use echo keyword to display result echo "Open console and check"; echo '<script>console.log("Welcome to GeeksforGeeks!"); </script>'; ?> |
Output:

Now, we will see how to write the value of PHP variables to the console line.
Example 3: This example illustrates how to print PHP variables value to the console.
<?php // Declare variable and store the string $var1 = "GeeksforGeeks"; $var2 = "A computer science portal"; echo "Open console and check"; echo '<script>console.log("'$var1 . "\n" . $var2'"); </script>'; ?> |
Output:

Recommended Posts:
- How to print debug messages in the Google Chrome JavaScript Console?
- console in JavaScript
- JavaScript | console.log() with Examples
- View the list of all variables in Google Chrome Console using JavaScript
- Difference between console.dir and console.log
- Difference between console.dir and console.log
- How to print the content of an object in JavaScript ?
- Print the content of a div element using JavaScript
- JavaScript | Window print() Method
- How to create a pop-up to print dialog box using JavaScript?
- Print current day and time using HTML and JavaScript
- How to pretty print JSON string in JavaScript ?
- How to print a number with commas as thousands separators in JavaScript?
- Node.js | console.log() Function
- Console.OpenStandardError Method in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



