Open In App

How to print on browser’s console using PHP ?

Last Updated : 26 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads