Is there any way to debug PHP via the browser console?

4

Is there any way to debug PHP via the browser console?

I sometimes want to debug a value on the system in production, but I do not want the user to notice it. Then I had the idea of using the browser console.

You can do something like this:

console_log(['nome' => 'Wallace']);

And in the console appear:

{nome: "wallace"}
    
asked by anonymous 06.11.2015 / 14:23

2 answers

8

I even asked the question and I answered because I never saw anyone with this curiosity.

There is a way, and I created it in this gist

The code is very simple. It is possible to pass infinite parameters to be debugged with this function.

Source Code:

function console_log()
{
    foreach (func_get_args() as $mixed) {
        printf('<script>console.log(%s)</script>', json_encode($mixed));
    }
}

The use of this would be like this:

console_log($_GET, $_POST, $_SERVER);

Then, you just open the console and see what happens;)

    
06.11.2015 / 14:23
1

A different form where you would not be able to insert into the console (if an error occurs after calling the function you created) but debugging externally would write the result of the variable in an external file like this:

ob_start();
var_dump($featuresArray);
$result = ob_get_clean();
$file = 'C:\PRINT_VAR_DUMP.txt';
file_put_contents($file, $result);

Content saved in this file can be opened in notepad ++.

    
27.11.2015 / 13:47