PHP Log to Console

In this tutorial, we are going to see the ways to log the errors or bugs in the console area of the browser in PHP, let’s start the tutorial with the introduction of PHP Log to Console.

Introduction to PHP Log and Console

PHP Log is the description related to a bug or an error, the PHP Log informs the developer where and why the error is generating. The information related to an error is shown in the console area of the browser. You can access it by right-clicking on the web browser page and going to inspect, a window will open then click on the console.  

The PHP log performs an important role in web programming, it allows a developer to show data in the console with bugs and errors so the developer can effortlessly perform debugging. 

Log to console enables a developer to correct their mistakes, solve irritating errors and make their application bug free. An error-free application is the desired application for all the developers and users as well.

Furthermore, there is also a weak point about the Console that is it does not show every error or bug, There is some sort of errors that the console does not display, to show all types of errors we have a few methods in PHP. The methods are explained below:

1)by using Json encode method

2)by using Json encode in a script tag

3)by using PHP libraries

JSON Encode Method

The JSON encode method is a built-in function in PHP that simply converts the array or an object into a JSON Format.  Let’s understand the following example.



print "<h2>PHP log to Console</h2>";
//Declaring method to show info in console 
function show_log_errors($e1, $e2)
    //addition of two numbers
    $add = $e1 + $e2;
    //Set the console output
    $res = "The sum of $e1 and $e2 is $add";
    //Assign javascript code into the variable
    $jscript = "<script> console.log(".json_encode($res, JSON_HEX_TAG)."); </script>";
    //Executing the js code using PHP
    echo $jscript;
//Assigning values for addition
$no1 = 87;
$no2 = 45;
//Calling the function
show_log_errors($no1, $no2);

Code Explanation

In the above code, we declared a function “show_log_errors” and pass two variables ($e1,$e2) as a parameter, then we declared a variable “$add” and assigned it the summation result of those two parameters, and then we declared another variable in which we stored the result of $e1 and $e2 variable.

In the next step, we used a script tag in which we declared the JSON encode method that will display the results in the console. We also declared the console.log function that will show the strings in the console area.

In the end, we just declared two random numbers for addition and then called our function ”show_log_errors” which will print the summation of two numbers and check out the results below.


The sum of 87 and 45 is 132

you can check the result in the console area of your browser.

Let’s move forward to the next method.

JSON Encode in a Script Tag


echo "<h3>log to console</h3>";
//Assigning a string to a variable
$message = "hello world";

//adding PHP variable values in Js variable. 
var js_var_val = <?php echo json_encode($message, JSON_HEX_TAG); ?>;
//Printing variable using console.log


Code Explanation

In the above example, we declared a variable and assigned it a string value, then opened the script tag and declared a variable in which we used our Json encode function.

You must be noticing that here we are using PHP echo instead of the console as we did in the last example, don’t be confused here we are trying to explain the concept that a PHP code can also be used in the console.log function inside the script tag, see the results below.


hello world

You can see the output in the console area of your browser page.

Let’s see our next method explained below.

Using PHPDebugConsole library

If you want to see the whole file logs in the console and want to experience extra features and wonderful performance then the PHPDebugConsole library is the best decision for you.

This library interacts with your application pretty easily it also checks each line of code and displays the hidden log bugs or errors in the console.


require '/path/to/Debug.php';

$debug = new \bdk\Debug(array(
    'collect' => true, // turn logging on
    'output' => true, // if left false (default), the output() method will return an empty string

 * Example Class
 * Demonstrates PHP reflection and PHPDocBlock
class Example
    private $offLimits = 'I\'m a private property';

    public function __toString() {
        return 'It\'s Magic';

     * Foo is a private method that does stuff
     * @param string $bar the string you want to foo
     * @return string the foo string
    private function foo($bar) {

$debug->info('PHPDebugConsole is great!');
$debug->warn('No browser plugin necessary');
$debug->log('features/options', array(
    'PHP port of the javascript web console API',
    'outputOpts' => array(
        'HTML (as seen here)',
        'Plain-text / file',
        '"plugin" (send log realtime via websockets, etc)',
    'password protected',
    'errors (even fatal) are captured / logged / displayed,
    'send error notices via email (throttled so as not to send out a flood of emails)',
    'send debug log via email',

Code Explanation

The above code is an example of the PHP Debug Console library from its official demo website you can take an idea from this code.

Before executing it make sure you have installed the library and some extensions.


In this tutorial, we talked about the methods of displaying PHP logs to the console.

We also discussed the introduction to PHP logs and console then we took a look at the methods with examples and discussed the codes as well.

That’s all for today hope you like the tutorial if you have any queries regarding this article so feel free to contact us below. Thanks for reading.

Leave a Comment