. Advertisement .
..3..
. Advertisement .
..4..
For any coder, PHP should be a popular server-side scripting language. It is so easy to use that it appears in most building web applications. How to print console using PHP? This article will offer you the answer.
What Is PHP Echo?
PHP echo is more a language construct than a Javascript function. Thus, it cannot be applied in a function’s context. When passing more than one parameter to it, you should avoid enclosing them within parentheses.
Use <script> tag to print the console.
Syntax:
echo "Welcome To The Morning Sun";
echo variable_name;
How To Log To Console In PHP
Use Json_encode function
<?php $view_variable = 'a string here'; ?>
<!-- some HTML content here -->
<div>
<!-- even more HTML content here -->
</div>
<?= console_log($view_variable); ?>
Generated HTML markup looks like:
<!-- some HTML content here -->
<div>
<!-- even more HTML content here -->
</div>
<script>console_log('a string');</script>
Log In The Middle Of Javascript Code
<script>
// other JavaScript code before ...
var js_variable_as_placeholder = <?= json_encode($view_variable,
JSON_HEX_TAG); ?>;
console.log(js_variable_as_placeholder);
// other JavaScript code and after ...
</script>
Use PHP Libraries
<?php
require __DIR__.'/vendor/autoload.php'; // if installed via composer
// require 'path/to/Debug.php'; // if not using composer
$debug = new \bdk\Debug(array(
'collect' => true,
'output' => true,
));
$debug->log('goodbye');
How To Print Console Using PHP
Print PHP Variables Value To The Console
<?php
// Declare variable and store the string
$var1="W3docs";
$var2="A computer science portal";
echo "Open console and check";
echo '<script>console.log("'$var1 . "\n" . $var2'"); </script>';
?>
Example:
Input:
<?php
// Declare variable and store the string
$var1 = "The Morning Sun";
$var2 = "Open To A New World";
echo "Open console and check";
echo '<script>console.log("'$var1 . "\n" . $var2'"); </script>';
?>
Output:
The Morning Sun
Open To A New World
Use Echo Keyword To Display Content
Input:
<?php
// Declare variable and store the string
$var1 = "The Morning Sun";
$var2 = "Open To A New World";
// Use echo keyword to display result
echo $var1 . "\n" . $var2 ;
?>
Output:
The Morning Sun
Open To A New World
It is worth noting that the dot operator can join two PHP strings. Thus, you can print various variables with a dot ‘.’ operator.
Print A String Into Console
There are two ways to write and print a string into the console. The first method involves using the console.log().
Input:
<?php
function write_to_console($data) {
$console = $data;
if (is_array($console))
$console = implode(',', $console);
echo "<script>console.log('Console: " . $console . "' );</script>";
}
write_to_console("Goodbye!");
write_to_console([4,5,6]);
?>
Output:
Console: Goodbye!
Console: 4,5,6
Using the console.long() along with the json_encode() function is also a great idea to write to the console in PHP.
Input:
<?php
function write_to_console($data) {
$console = 'console.log(' . json_encode($data) . ');';
$console = sprintf('<script>%s</script>', $console);
echo $console;
}
$data = [ 'foo' => 'bar' ];
$days = array("Mon", "Tue", "Fri");
write_to_console($data);
write_to_console($days);
?>
Output:
Object { foo: "bar" }
Array(3) [ "Mon", "Tue", "Fri" ]
Conclusion
This article has offered various methods on how to print console using PHP. Mastering these techniques will save you a great deal of time when designing your website.
Leave a comment