Is there any way to debug PHP via browser console?

Asked

Viewed 1,090 times

4

Is there any way to debug PHP via browser console?

I sometimes want to debug a value on the system in production, but I wouldn’t want the user to notice. Then I got the idea to use the browser console.

It’s possible to do something like this:

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

And on the console appear:

{nome: "wallace"}

2 answers

8

I even asked the question and answered, because I’ve never seen anyone with such 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 it would be like this:

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

Then just open the console and see what happens ;)

inserir a descrição da imagem aqui

  • Very good idea to post here, I’ve done this several times, but always found it a master monster gambiarra but breaks a branch.

  • Very interesting, but I thought it would be something to pause the code. This wouldn’t just be data display?

  • I call debugging also display the variable value (as in var_dump)

  • 1

    you do not like the Xdebug no?

  • It depends. Sometimes he fills the bag with the business of Maximum call 100, in recursion

  • @Wallacemaxters I think I saw this in this post: http://www.paulund.co.uk/output-php-data-in-browser-console to debug a production result is good, but still anyone with access to the console could see, tested some plugins pro Chrome as https://Chrome.google.com/webstore/Detail/php-console/nfhmhhlpflenkpnnnkolmclajemef and did not produce good results...

  • @Wallacemaxters take a look at this one: http://www.codeforest.net/debugging-php-in-browsers-javascript-console looks much more complete.

  • @Gabrielrodrigues, I prefer to use json_encode in all values. You can see the true, false and null. PHP will print a blank if you follow the link example

Show 3 more comments

1

A different way where it would not be possible to insert in the console (in case any error occurs after the call of the function you created) but debugging externally would be to write the result of the variable in an external file in this way:

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

The contents saved in this file can be opened in Notepad++ (idented).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.