29
For example, in javascript we have the console.log
to debug, find out where the bugs are, etc. And in PHP, which ones would be best manners?
29
For example, in javascript we have the console.log
to debug, find out where the bugs are, etc. And in PHP, which ones would be best manners?
21
Consider using the function debug_backtrace
PHP, but only in development, because the function has a great computational cost.
Documentation: http://php.net/debug_backtrace
Use like 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));
exit;
}
}
// ...
if (/* ... */) {
debug();
/*
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 adaptations to your case, the function looks great to debug the code!
See also:
var_dump
- http://br2.php.net/manual/en/function.var-dump.phpprint_r
- http://br2.php.net/manual/en/function.print-r.phpvar_export
- http://br1.php.net/var_exportdebug_print_backtrace
- http://br1.php.net/debug_print_backtrace__LINE__
and __FILE__
- http://br1.php.net/manual/en/language.constants.predefined.phpIf you are going to quote this question, it is good to also comment on the debug_print_backtrace
. Another point is that console.log
javascript is different from debug_backtrace
. One prints a variable, while the other prints the path to where it is executed. If you only quote these points you get my +1 for making your answer clearer.
9
In PHP, the direct equivalent of console.log
would be print_r
, var_dump
and the var_export
.
If you have the xdebug
enabled, can also use the xdebug_var_dump
which displays the output mode also pre-formatted and colored.
Now, what about the best debugging mode? Well, it depends on each case. It can be by printing the screens as I said, or with an integrated IDE with xdebug
or pre-defined equivalent and may even set breakpoints.
It is also possible to 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.
Answer step by step this is reason for another exclusive question, but a reference to know more about it can be seen in Debugging PHP Source Code in Netbeans IDE
Although it is the most advanced medium, it can be complex to configure and more time consuming than testing with print_r and die. But it is excellent in more complex applications.
7
In php we have the var_dump()
which returns the element to be debugged and all its properties.
Mentioning the php documentation the var_dump()
Dve be used like this:
ExamplesExample #1 Example from var_dump()
<?php
$a = array (1, 2, array ("a", "b", "c"));
var_dump ($a);
?>The above example will print:
/* mostrará: array(3) { [0]=> int(1) [1]=> int(2) [2]=> array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" } }Example #2 Example from var_dump()
<?php
$b = 3.1;
$c = true;
var_dump($b,$c);
?>The above example will print:
float(3.1) bool(true)
6
The most practical option to debug in PHP is to install a debbuger as Xdebug or Zend Debugger in conjunction with an IDE (Eclipse, Netbeans, Phpstorm etc) this combination allows:
That PHP code be paused at any time with a breakpoint.
Inspection of variables and their values.
It is possible to profile performance, which helps to efficiently detect bottlenecks or slowness.
Go to http://xdebug.org/ download the specific version of your php and throw the file in the extensions folder (usually ext).
Open php.ini and add the following lines and restart apache to put the changes into effect.
[XDebug]
zend_extension = "php_xdebug.dll"
xdebug.remote_handler = "dbgp"
xdebug.remote_host = "localhost"
xdebug.remote_port=9000
xdebug.remote_enable = On
Thus the Xdebug will be activated via demand, ie it is necessary to inform the query string XDEBUG_SESSION_START
on all pages you want to enable it. To facilitate the process there are extensions for Firefox (Xdebug cute) and Chrome (Xdebug helper) who do this job.
If you want to enable debug on all requests made add this line:
xdebug.remote_autostart=On
The option xdebug.remote_autostart=On
is useful if you are testing something via console. I did not test to see if the impact on local performance is too big, but it is much more practical :) Even via browser and with the add-on to enter the cookie fills the bag.
@gmsantos it runs slower, if u use the phpmyadmin a ide pause the code until give 'resume', it is kind of uncomfortable. I liked the add-on more.
3
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.
3
I don’t know where you are developing, but if it were on a Linux system it would be nice for you to disable errors by php.ini
changing the keys expose_php = off
and display_erros = off
. So your system would be safer and could view kernel errors.
By Ubuntu you would use the following command:
$: tail -f /var/log/apache2/error.log
2
Place this line at the point you want to debug:
echo "TRACE at:".__FILE__.":".__LINE__.":<br\><pre>". debug_string_backtrace()."</pre>"; exit;
Function debug_string_backtrace() { ob_start(); debug_print_backtrace(); $trace = ob_get_contents(); ob_end_clean(); // Remove first item from backtrace as it’s this Function which // is Redundant. $trace = preg_replace ('/ #0 s+' . FUNCTION . "[^\n]* n/", '', $trace, 1); // Renumber backtrace items. $trace = preg_replace ('/ #( d+)/me', '#' . ($1 - 1)', $trace); Return $trace; }
1
A strategy that works in almost every environment is writing a log file:
<?php
$test = 'My variable value';
file_put_contents('./my-log.txt', 'test='.$test);
0
If you want to know the value of a variable, you can only use:
die(var_dump($variavel));
Browser other questions tagged php debug
You are not signed in. Login or sign up in order to post.
There are several debuggers for PHP ready to use. Generally, filling everything with prints is not the best solution, whether in PHP or in other languages. Breakpoints/Watches/etc. are better (I don’t know the Portuguese translation of this)
– luiscubal
I use Xdebug, and a plugin to use it in Sublime Text 3 (add breakpoints, etc).
– Calebe Oliveira
Guys, I edited the question to prevent it from being closed down because it’s based on personal opinions. Interested parties look at the editing history and, if necessary, improve or reverse any changes
– Emerson Rocha
Instead of
var_dump
, often use:echo '<pre>' . print_r( $variavel, true ) . '</pre>';
– brasofilo