How to debug code in PHP?


For example, in javascript we have console.log to debug, find out where the errors are, etc. And in PHP, what would be the best ways ?

asked by anonymous 17.02.2014 / 00:05

9 answers


Consider using the debug_backtrace function of PHP, but only in development, because the function has a great computational cost.

Documentation: link

Use this:

$is_dev = true;

function debug() {
    global $is_dev;

    if ($is_dev) {
        $debug_arr = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
        $line = $debug_arr[0]['line'];
        $file = $debug_arr[0]['file'];

        header('Content-Type: text/plain');

        echo "linha: $line\n";
        echo "arquivo: $file\n\n";
        print_r(array('GET' => $_GET, 'POST' => $_POST, 'SERVER' => $_SERVER));

// ...

if (/* ... */) {

    Nesse ponto, a execução do script será interrompida
    e serão impressos o nº da linha atual, o nome do arquivo e as variáveis
    $_GET, $_POST e $_SERVER

Making some adjustments to your case, the function is great for debugging the code!

See also:

17.02.2014 / 00:18

In PHP, the direct equivalent of console.log would be print_r , var_dump and var_export .

If you have xdebug enabled, you can also use xdebug_var_dump which displays the output in both pre-formatted and color mode.

Now, what about the best way to debug? Well, that depends on each case. You can either print the screens as I said, or with an integrated IDE with xdebug or equivalent predefined and can even set breakpoints.

Short summary with key differences

  • print_r : simpler. Does not export with types
  • var_dump : more detailed. Export types. Useful in cases where it has values 0, '', NULL.
  • var_export : looks like var_dump but exports a code that can be reused in your Script.


You can also debug an application during code execution. For this you will need an IDE and some module that, when your script runs, allows any tool that connects to a specific port to interact with your script.

Respond step-by-step this is a reason for another unique question, however a reference to know more about it can be seen at Debugging the PHP Source Code in NetBeans IDE

Although it is the most advanced medium, it can be complex to set up and takes longer than testing with print_r and die. But it's great for more complex applications.

17.02.2014 / 00:13

In we have the var_dump() that returns the element to be debugged and all its properties.

Mentioning the php documentation % d_% is used like this: / p>


Example # 1 var_dump () var_dump()

The above example will print:

/* mostrará:
array(3) {
  array(3) {
    string(1) "a"
    string(1) "b"
    string(1) "c"

Example # 2 var_dump () <?php
$a = array (1, 2, array ("a", "b", "c"));
var_dump ($a);

The above example will print:

17.02.2014 / 00:12

The most practical option for debugging in PHP is to install a debugger like xdebug or Zend Debugger in conjunction with an IDE (Eclipse, NetBeans, PhpStorm etc)

  • Let PHP code be paused at any time with a breakpoint.

  • Inspection of variables and their values.

  • It is possible to make performance profiling, which helps you to efficiently detect bottlenecks or slowness.

Install xdebug

Go to link download the specific version of your php and play the file in the extensions folder (usually ext).


Open php.ini and add the following lines and restart apache to make the changes effective.

zend_extension = "php_xdebug.dll"
xdebug.remote_handler = "dbgp"
xdebug.remote_host = "localhost"
xdebug.remote_enable = On

So Xdebug will be activated on demand, ie it is necessary to inform the query string XDEBUG_SESSION_START on all the pages that you want to activate it. To facilitate the process there are extensions to Firefox ( xdebug cute ) and Chrome (< a href=""> xdebug helper ) that do this work.

If you want to enable debug on all requests made add this line:

20.09.2016 / 14:36

I do not know where you are developing, but if it were on a Linux system it would be nice to disable the errors by php.ini by changing the keys expose_php = off and display_erros = off . So your system would be more secure and could see kernel errors.

For Ubuntu you would use the following command:

$: tail -f /var/log/apache2/error.log
17.02.2014 / 14:50

You can use a lib called dephpugger. It's like ipdb for python or byebug for ruby. You type xdebug_break () in your code and it stops at the terminal.


24.03.2017 / 15:06

If you want to know the value of a variable, you can only use:

19.02.2014 / 03:03

Place this line at the point you want to debug:

echo "TRACE at:".__FILE__.":".__LINE__.":<br\><pre>". debug_string_backtrace()."</pre>"; exit;
19.09.2017 / 22:30

One strategy that works in almost every environment is writing a log file:

$test = 'My variable value';
file_put_contents('./my-log.txt', 'test='.$test);
20.02.2018 / 17:52