Author Topic: How to debug in PHP  (Read 3647 times)

0 Members and 1 Guest are viewing this topic.

Offline m0l0ko

  • Peasant
  • *
  • Posts: 129
  • Cookies: -4
    • View Profile
How to debug in PHP
« on: March 09, 2013, 04:31:47 pm »
I just use a simple IDE (geany) to write my PHP scripts and these days error reporting doesn't even work for me half the time (don't know why, most of the time I just get a blank screen, even when I add code to turn error reporting on) so when something goes wrong, first I'll just eye scan the code and look for syntax errors, then if that fails I start hitting the undo button and reloading the page until the problem goes away, and like that I isolate the problem. This becomes a big problem if I've made loads of changes to the scripts because I have no idea how far I have to go back to isolate the problem.

 I'm guessing this is a ridiculous way to do things. I need to learn how to debug and error check. I installed the Firebug + FirePHP plugin for firefox but it doesn't tell me anything. Heres an example. I put in a syntax error (an extra ' inside the echo '' quotes):



so I reload the page and PHP doesn't tell me the problem, I just get a blank screen:



which I don't understand because error reporting is on. It tells me about undefined variables and stuff like that, but it won't tell me about a syntax error? Anyhow, I open up Firebug and see if it tells me the problem:



but as you can see, it tells me nothing. What am I doing wrong?
« Last Edit: March 09, 2013, 05:06:01 pm by m0l0ko »

Offline DaNePaLI

  • Peasant
  • *
  • Posts: 55
  • Cookies: 12
  • Forever n00b
    • View Profile
Re: How to debug in PHP
« Reply #1 on: March 09, 2013, 05:01:38 pm »
Make sure the display_errors = On is set in your php.ini configuration file. Moreover, I personally prefer error_reporting = E_ALL & ~E_NOTICE as this provides even the small notices in your script. Likewise, you can use var_dump(), print_r(), and normal print()/echo() statements to ease your debugging process.

And, once you get familiar with PHP, I would recommend you to use some good IDEs with debugging support. I personally use Zend IDE (commercial IDE but there's lots of cracks out there) and its awesome. I guess there are other freely available tools that support debugging.

Offline wookie

  • Peasant
  • *
  • Posts: 68
  • Cookies: -4
    • View Profile
Re: How to debug in PHP
« Reply #2 on: March 09, 2013, 05:22:29 pm »
Check your log files. You can turn error logging on in your php.ini file.

Scrap the frameworks if you're just starting out, get a LAMP environment set up and play around.

First place to check is the logs.

Try adding a function into your code like

Code: [Select]
function print_r_pre($ary){
print("<pre>");
print_r($ary);
print("</pre>:);
}

That should present arrays and objects in a much more readable format in your browser.

Offline m0l0ko

  • Peasant
  • *
  • Posts: 129
  • Cookies: -4
    • View Profile
Re: How to debug in PHP
« Reply #3 on: March 09, 2013, 07:05:29 pm »
I added this:
Code: [Select]
error_reporting = E_ALL & ~E_NOTICE
display_errors = On
to both php.ini files (on Ubuntu theres a /etc/php5/cli/php.ini and /etc/php5/apache2/php.ini, I don't know which one it uses so I just edited both) and restarted the apache server but it did nothing. Then I added this:
Code: [Select]
ini_set('display_errors', 1);
error_reporting(E_ALL);
to the top of my script but it still did nothing. I don't know what the hells wrong here. I have a LAMP server.

I do use var_dump() and print_r() for to find out whats going on with arrays and objects. Theres so many things I can't get any feedback for though, one example is when I include a file with include("/path/file") if the file doesn't exist, it would be useful if it would display the full path of the file which it is trying to include, that way I could see exactly why it can't find it. Instead I have to play around with the path (i.e. ../../path/file) until it can find it.
« Last Edit: March 09, 2013, 07:11:53 pm by m0l0ko »

Offline wookie

  • Peasant
  • *
  • Posts: 68
  • Cookies: -4
    • View Profile
Re: How to debug in PHP
« Reply #4 on: March 10, 2013, 11:11:45 pm »
Search for error_log in your php.ini file and where it asks for the file, just uncomment it and change it to something like /var/log/php_errors.log and restart apache.

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: How to debug in PHP
« Reply #5 on: March 11, 2013, 06:21:46 am »
As far as I know there are no debuggers for PHP(that works good anyway). When you get a blank page because of syntax error, the error is so bad it should be obvious and easy to spot it.

From the looks of it, you have fucked up your singe and double quote encapsulation. Singe quotes can surround double quotes and vica versa but only if the data you want to encapsulate does not contain the quote type you are encapsulating it with. However, you can mix the two with escape characters and or mixing multiple strings and adding them together.

Examples:

echo("<html>"); - No problem

echo("<html var="value">"); - Problem, encapsulated data contains the encapsulating quote type.
Solution:
echo('<html var="value">'); OR echo("<html var" . '"value">');

echo('<tag onclick="alert('Hello world');">'); - Problem, same as above.
Solution:
echo('<tag onclick="alert(\'Hello world\');">'); OR echo('<tag onclick="alert('. "'Hello world'" .')">');




Further more, you can do manual debugging or bug hunting if you like. Echo and die throughout your script until you see where things crash. However, this will not work if you have a too bad of an error. Alternatively, remove as big parts of code as you can and put each part of what you removed back into place until things crash again.
« Last Edit: March 11, 2013, 06:21:59 am by ande »
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true