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.
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
<?php echo "<h3>log to console</h3>"; //Assigning a string to a variable $message = "hello world"; ?> <script> //adding PHP variable values in Js variable. var js_var_val = <?php echo json_encode($message, JSON_HEX_TAG); ?>; //Printing variable using console.log console.log(js_var_val); </script>
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.
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.
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.