Home > Backend Development > PHP Problem > How to return data to the console in php

How to return data to the console in php

(*-*)浩
Release: 2023-02-26 17:00:02
Original
8115 people have browsed it

How to return data to the console in php

When debugging our PHP program, a common way is to print the variable information to the browser, for example: (recommended learning: PHP video tutorial)

   <?php  
    echo &#39;<pre class="brush:php;toolbar:false">&#39;;  
    print_r($bar);  
    echo &#39;
'; exit;
Copy after login

But printing it out directly may interfere with your page, disrupt the layout, affect the data returned as Api and other issues. At this time, we can output the debugging information to a file. The following is the simplest example:

error_log(print_r($bar, true));
Copy after login

However, this method is suitable for recording logs, but it is still not suitable for debugging. convenient. At this time, we can use the console of modern browsers, such as Chrome Console (Win shortcut key Ctrl Shift J), to output the content to the Console, which can solve this problem.

There are already some such tools, such as PHP-Console or Chrome Logger. The former is for PHP, and the latter supports multiple server-side languages. The method of use is to install a Chrome extension first. Then provide the server-side library for calling. The implementation method is generally to use Session, Cookies, etc. to transfer data.

The above are relatively complete solutions and provide rich functions. But if you just want a simple, usable way to solve this problem without installing an extension, then you can do it like this:

<?php  
    function console_log($data)  
    {  
        if (is_array($data) || is_object($data))  
        {  
            echo("<script>console.log(&#39;".json_encode($data)."&#39;);</script>");  
        }  
        else  
        {  
            echo("<script>console.log(&#39;".$data."&#39;);</script>");  
        }  
    }
Copy after login

It is essentially adding a javascript script to the page. Use the console.log(); function to output information to the console. The above method will print out the information in the form of strings. If the single quotes are removed, PHP arrays and objects will be printed out in the form of JS objects, that is,

echo("<script>console.log(".json_encode($data).");</script>");
Copy after login

The above is the detailed content of How to return data to the console in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template