Log to Console Using PHP
The log information can be displayed to the console in two ways by using a PHP script. One is to use json_encode() function, and another is to use PHP libraries. The json_encode() function has, in this tutorial, to send data into the console. The syntax of this function is given below.
Syntax
Here, the first argument is a mandatory argument, and the other two arguments are optional. The first argument contains the value that will be encoded. The second argument contains the bitmask value that can be used to modify the output of the function. The third argument is used to define the depth value that must be greater than 0. It returns the encoded string upon success or false upon failure.
Different Uses of the Console on PHP
The json_encode() function is used in different ways to display data in the browser console.
Example 1: Use of json_encode() Function to Display String Data Into the Console
Create a PHP file with the following script to display the sum of two numbers as a string into the console using the json_encode() function. The console.log() function of JavaScript has been used in the script to display the string data into the console. The display_log() function with two parameters has been defined to create a string value after calculating the sum of the values and displaying the encoded data into the console using the json_encode() function. The JSON_HEX_TAG value has been used in the second argument of the json_encode() function to convert all “<” and “>” to \u003C and \u003E:
echo "<h3>Console Example-1</h3>";
//Define function to print data in the console
function display_log($n1, $n2)
{
//Calculate the sum of two numbers
$sum = $n1 + $n2;
//Set the console output
$output = "The sum of $n1 and $n2 is $sum";
//Assign javascript code into the variable
$js_code = "<script> console.log(".json_encode($output, JSON_HEX_TAG)."); </script>";
//Excute the javascript code using PHP
echo $js_code;
}
//Assign two numeric values
$num1 = 25;
$num2 = 35;
//Call the function with two numbers
display_log($num1, $num2);
?>
Output
The following output will appear after executing the previous script:
Example 2: Use of json_encode() Function to Display Array Values Into the Console
Create a PHP file with the following script to display a simple numeric array into the console using json_encode() function and console_log() function of JavaScript. The array_log() function has been defined with a parameter to display the encoded data of the PHP array into the console by using the json_encode() function. The JSON_HEX_TAG value has been used in the second argument of the json_encode() function as the previous example for the same purpose:
echo "<h3>Console Example-2</h3>";
//Declare an numeric array
$numArray = [56, 78, 23, 21, 50, 61];
//Declare a function to print array into the console
function array_log($data){
//Assign javascript code into the variable
$js_code = "<script> console.log(".json_encode($data, JSON_HEX_TAG)."); </script>";
//Excute the javascript code using PHP
echo $js_code;
}
//Call the function
array_log($numArray)
?>
Output
The following output will appear after executing the script above. The total number of array values has been printed in the console also:
Example 3: Use of json_encode() Function Inside <script> Tag
In the previous two examples, the console_log() function of JavaScript has been used inside the echo. The encoded data of the PHP can be used inside the JavaScript console.log() function by using the <script> tag. Create a PHP file with the following script to display the string value into the console using JavaScript. Here, the encoded value generated by the json_encode() function of PHP has been stored into a JavaScript variable, and this variable has been printed in the console by using the console.log() function:
echo "<h3>Console Example-3</h3>";
//Assign a string variable
$strValue = "Welcome to Linuxhint";
?>
<script>
//Set the value of the PHP variable into the JavaScript variable
var js_var = <?php echo json_encode($strValue, JSON_HEX_TAG); ?>;
//Print the variable in the console
console.log(js_var);
</script>
Output
The following output will appear after executing the previous script:
Example 4: Use of json_encode() Function to Display Multiple Lines into the Console
Create a PHP file with the following script to display multiple lines into the console using json_encode() function and console_log() function of JavaScript. The logData() function has been defined with a parameter to display a simple string and the encoded data of the PHP into the console in two lines. The JSON_PRETTY_PRINT value has been used in the second argument of the json_encode() function to display the formatted output:
echo "<h3>Console Example-4</h3>";
function logData($data) {
//Print the first line in the console
echo "<script>console.log('---Log Information---');</script>";
$message = json_encode($data, JSON_PRETTY_PRINT);
//Print the second line in the console
echo "<script>console.log(" . $message . ");</script>";
}
$value = 'Testing Log Data';
logData($value);
?>
Output
The following output will appear after executing the previous script:
Example 5: Use of json_encode() Function With func_get_args() Function
The func_get_args() function is used to read the argument list of the function in an array. Create a PHP file with the following script to display the array of argument values of the function into the console by using the func_get_args() function. The console() function has been defined without any argument in the script. However, four argument values have been given at the time of calling the console() function. The func_get_args() function has been used in the first argument of the json_encode() function to generate the encoded data of the argument array:
echo "<h3>Console Example-5</h3>";
function console()
{
//Read argument values and create javascript code
$js_code = '<script> console.log('.json_encode(func_get_args(), JSON_HEX_TAG).'); </script>';
//Print the output in the console
echo $js_code;
}
//Call the function
console('Test Data', 56.78, 34, 45.88);
?>
Output
The following output will appear after executing the previous script.
Conclusion
The different ways to print data into the browser console using the json_encode() function have been shown in this tutorial. This is the most simple way of using a browser console. I hope the new PHP users will be able to write data correctly into the console after reading this tutorial properly. Follow Linux Hint for more tips and information.