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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Declare variable and store the string
$var1 = "GeeksforGeeks";
$var2 = "A computer science portal";
  
// Use echo keyword to display result
echo $var1 . "\n" . $var2 ;
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Use echo keyword to display result
echo "Open console and check";
  
echo '<script>console.log("Welcome to GeeksforGeeks!"); </script>';
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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>';
?>

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.